function box(varargin)
%BOX    Axis box.
%   BOX ON adds a box to the current axes.
%   BOX OFF takes if off.
%   BOX, by itself, toggles the box state of the current axes.
%   BOX(AX,...) uses axes AX instead of the current axes.
%
%   BOX sets the Box property of an axes.
%
%   See also GRID, AXES.

%   Copyright 1984-2019 The MathWorks, Inc.

% To ensure the correct current handle is taken in all situations.

isaxarr=matlab.graphics.chart.internal.objArrayDispatch(@box,varargin{:});
if isaxarr
    return
end

opt_box = 0;
if nargin == 0
    ax = gca;
else
    arg1=varargin{1};
    if isempty(arg1)
        opt_box = lower(arg1);
    end
    if matlab.graphics.internal.isCharOrString(arg1)
        % string input (check for valid option later)
        if nargin == 2
            error(message('MATLAB:box:HandleExpected'))
        end
        ax = gca;
        opt_box = lower(arg1);
    else
        % handle must be a handle and axes handle
        if ~isempty(arg1) && (any(~ishghandle(arg1),'all') || any(~isprop(arg1,'Box'),'all'))
            error(message('MATLAB:box:ExpectedAxesHandle'));
        end
        ax = arg1;
        
        % check for string option
        if nargin == 2
            opt_box = lower(varargin{2});
            if isscalar(opt_box) && islogical(opt_box)
                if opt_box
                    opt_box = 'on';
                else
                    opt_box = 'off';
                end
            end
        end
    end
end

if (isempty(opt_box))
    error(message('MATLAB:box:UnknownOption'));
end

matlab.graphics.internal.markFigure(ax);

if (char(opt_box) == 0)
    if (strcmp(get(ax,'Box'),'off'))
        set(ax,'Box','on');
    else
        set(ax,'Box','off');
    end
elseif (strcmp(opt_box, 'on'))
    set(ax,'Box', 'on');
elseif (strcmp(opt_box, 'off'))
    set(ax,'Box', 'off');
else
    error(message('MATLAB:box:CommandUnknown'));
end
