function Stop = plotObjectiveEvaluationTime(bo, State)
%plotObjectiveEvaluationTime     PlotFcn for plotting each
%observed Objective function evaluation time versus the number
%of function evaluations.
%   Stop = plotObjectiveEvaluationTime(bo, State) plots each
%   observed Objective function evaluation time 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('FEvalTimeTitle');
XLabel = bayesoptim.infoString('FEvals');
YLabel = bayesoptim.infoString('FEvalTime');
Stop = false;
set(0, 'defaultfigureunits', 'pixels');     % Make sure units are pixels
switch State
    case 'initial'
        f = figure('Tag','bayesopt.ObjectiveEvaluationTime');
        Axes = axes(f);
        title(Axes, Title);
        xlabel(Axes, XLabel);
        ylabel(Axes, YLabel);
        Curve = [];
    case {'iteration', 'done'}
        delete(Curve);
        if isvalid(Axes)
            Curve = plotCurve(bo.ObjectiveEvaluationTimeTrace, Axes);
        end
    case 'standalone'
        f = figure('Tag','bayesopt.ObjectiveEvaluationTime');
        StandaloneAxes = axes(f);
        title(StandaloneAxes, Title);
        xlabel(StandaloneAxes, XLabel);
        ylabel(StandaloneAxes, YLabel);
        plotCurve(bo.ObjectiveEvaluationTimeTrace, StandaloneAxes);
end

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