function Stop = plotObjective(bo, State)
%plotObjective     PlotFcn for plotting each observed Objective
%function value versus the number of function evaluations.
%   Stop = plotObjective(bo, State) plots each observed
%   Objective function value versus the number of function
%   evaluations.
%
% See also: BAYESOPT, BAYESIANOPTIMIZATION
%

%   Copyright 2016-2022 The MathWorks, Inc.

persistent Axes Curve;
narginchk(2,2);
Title = bayesoptim.infoString('ObjFunTitle');
XLabel = bayesoptim.infoString('FEvals');
YLabel = bayesoptim.infoString('ObjFun');
Stop = false;
set(0, 'defaultfigureunits', 'pixels');     % Make sure units are pixels
switch State
    case 'initial'
        f = figure('Tag','bayesopt.Objective');
        Axes = axes(f);
        title(Axes, Title);
        xlabel(Axes, XLabel);
        ylabel(Axes, YLabel);
        Curve = [];
    case {'iteration', 'done'}
        delete(Curve);
        if isvalid(Axes)
            Curve = plotCurve(bo.ObjectiveTrace, Axes);
        end
    case 'standalone'
        f = figure('Tag','bayesopt.Objective');
        StandaloneAxes = axes(f);
        title(StandaloneAxes, Title);
        xlabel(StandaloneAxes, XLabel);
        ylabel(StandaloneAxes, YLabel);
        plotCurve(bo.ObjectiveTrace, StandaloneAxes);
end

    function Curve = plotCurve(ObjectiveTrace, Axes)
        HoldOff = bayesoptim.holdOn(Axes);
        Curve = plot(Axes, 1:numel(ObjectiveTrace), ObjectiveTrace, '-bo', 'MarkerSize', 2);
    end

end
