function grid(arg1, arg2);
%GRID   Grid lines.
%   GRID ON adds grid lines to the current axes.
%   GRID OFF takes them off.
%   GRID, by itself, toggles the grid state of the current axes.
%   GRID(AX,...) uses axes AX instead of the current axes.
%
%   GRID sets the XGrid, YGrid, and ZGrid properties of
%   the current axes.
%
%   See also TITLE, XLABEL, YLABEL, ZLABEL, AXES, PLOT.

%   Copyright 1984-2000 The MathWorks, Inc. 
%   $Revision: 5.8 $  $Date: 2000/07/30 12:50:34 $

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

opt_grid = 0;
if nargin == 0
	ax = gca;
else
	if isempty(arg1)
		opt_grid = lower(arg1);
	end
	if ischar(arg1)
		% string input (check for valid option later)
		if nargin == 2
			error('First argument must be an axes handle.')
		end
		ax = gca;
		opt_grid = lower(arg1);
	else
		% make sure non string is a scalar handle
		if length(arg1) > 1
			error('Axes handle must be a scalar');
		end
		% handle must be a handle and axes handle
		if ~ishandle(arg1) | ~strcmp(get(arg1, 'type'), 'axes')
			error('First argument must be an axes handle.');
		end
		ax = arg1;
		
		% check for string option
		if nargin == 2
			opt_grid = lower(arg2);
		end
	end
end

if (isempty(opt_grid))
    error('Unknown command option.');
end

if (opt_grid == 0)
	if (strcmp(get(ax,'XGrid'),'off'))
		set(ax,'XGrid','on');
	else
		set(ax,'XGrid','off');
	end
	if (strcmp(get(ax,'YGrid'),'off'))
		set(ax,'YGrid','on');
	else
		set(ax,'YGrid','off');
	end
	if (strcmp(get(ax,'ZGrid'),'off'))
		set(ax,'ZGrid','on');
	else
		set(ax,'ZGrid','off');
	end
elseif (strcmp(opt_grid, 'on'))
	set(ax,'XGrid', 'on');
	set(ax,'YGrid', 'on');
	set(ax,'ZGrid', 'on');
elseif (strcmp(opt_grid, 'off'))
	set(ax,'XGrid', 'off');
	set(ax,'YGrid', 'off');
	set(ax,'ZGrid', 'off');
else
	error('Unknown command option.');
end
