function stop = psplotbestx(optimValues,flag)
    %PSPLOTBESTX PlotFcn to plot best X value.
    %   STOP = PSPLOTBESTX(OPTIMVALUES,FLAG) where OPTIMVALUES is a structure
    %   with the following fields:
    %
    %   PATTERNSEARCH:
    %              x: current point X
    %      iteration: iteration count
    %           fval: function value
    %       meshsize: current mesh size
    %      funccount: number of function evaluations
    %         method: method used in last iteration
    %         TolFun: tolerance on function value in last iteration
    %           TolX: tolerance on X value in last iteration
    %
    %   FLAG: Current state in which PlotFcn is called. Possible values are:
    %           init: initialization state
    %           iter: iteration state
    %           done: final state
    %
    %   STOP: A boolean to stop the algorithm.
    %
    %   See also PATTERNSEARCH, OPTIMOPTIONS.


    %   Copyright 2003-2023 The MathWorks, Inc.

    % Always return a "stop" flag of false
    stop = false;

    % Persistent variables
    persistent thePlot

    switch flag
        case "init"
            thePlot = matlab.internal.optimfun.plotfcns.Factory.psplotbestx(optimValues);
        case "iter"
            thePlot.update(optimValues);
    end
end
