ファイル名:Zaxis.m

function [ans1, ans2, ans3] = zaxis(arg1, arg2, arg3, arg4);
% ZAXIS     Control zaxis scaling and appearance.
%   TIGHT   set(gca,'XLim','YLim')のとこだけ変えていて範囲の計算はXYZ軸全てで計算している。
%   FILL    何もしていない。
%
%   ZAXIS([ZMIN ZMAX])  sets scaling for the z-axes on the current plot.
%   V = ZAXIS  returns a row vector containing the scaling for the current plot. If the
%       current view is 2-D, V has four components; if it is 3-D, V has six components.
%   ZAXIS AUTO  returns the zaxis scaling to its default, automatic mode where, for each
%       dimension, 'nice' limits are chosen based on the extents of all line, surface, patch,
%       and image children.
%   ZAXIS MANUAL  freezes the zaxis scaling at the current limits, so that if HOLD is turned on,
%       subsequent plots will use the same limits.
%   ZAXIS TIGHT  sets the zaxis limits to the range of the data.
%   ZAXIS FILL  sets the axis limits and PlotBoxAspectRatio so that the axis fills the
%       position rectangle.  This option only has an effect if PlotBoxAspectRatioMode or
%       DataAspectRatioMode are manual.
%   ZAXIS IJ  puts MATLAB into its "matrix" axes mode.  The coordinate system origin is at
%       the upper left corner.  The i axis is vertical and is numbered from top to bottom.
%       The j axis is horizontal and is numbered from left to right.
%   ZAXIS XY  puts MATLAB into its default "Cartesian" axes mode.  The coordinate system
%       origin is at the lower left corner.  The x axis is horizontal and is numbered from
%       left to right.  The y axis is vertical and is numbered from bottom to top.
%   ZAXIS EQUAL  sets the aspect ratio so that equal tick mark increments on the x-,y- and
%       z-axis are equal in size. This makes SPHERE(25) look like a sphere, instead of an
%       ellipsoid.
%   ZAXIS IMAGE  is the same as ZAXIS EQUAL except that the plot box fits tightly around
%       the data.
%   ZAXIS SQUARE  makes the current axis box square in size.
%   ZAXIS NORMAL  restores the current axis box to full size and removes any restrictions on
%       the scaling of the units. This undoes the effects of ZAXIS SQUARE and ZAXIS EQUAL.
%   ZAXIS OFF  turns off all axis labeling, tick marks and background.
%   ZAXIS ON  turns axis labeling, tick marks and background back on.
%
%   See also AXES.

% ----------------------------------------------------------------------------------------------
%   Copyright (c) 1984-97 by The MathWorks, Inc.
%   $Revision: 5.29 $  $Date: 1997/04/08 06:08:06 $
% ----------------------------------------------------------------------------------------------
ax = gca;
pbarlimit = 0.1;
if (nargin == 0)                                    % 入力引数がないときは現在の軸の範囲を返す
    ans1 = [get(ax,'XLim') get(ax,'YLim') get(ax,'ZLim')];  % zaxisだけは常にZ軸が表示されるようにする
%   ans1 = [get(ax,'XLim') get(ax,'YLim')];
%   v = get(ax,'View');
%   if (v ~= [0 90])
%       ans1 = [ans1 get(ax,'ZLim')];
%   end
elseif (nargin == 1 & ~isstr(arg1))                 % 入力引数が1個でそれが文字でない時は[zmin zmax]だと判断する
    if (length(arg1) == 2)
        set(ax,'ZLim',arg1(1:2),'ZLimMode','manual');
        if (length(arg1) == 2 & get(ax,'View') == [0 90] & ~ishold) % ここもzaxis特有の設定行である
            view(3);
        end
    else
        error('Vector must have 2 elements.');
    end
%   if (length(arg1) == 4) | (length(arg1) == 6)
%       set(ax,'XLim',arg1(1:2),'YLim',arg1(3:4),'XLimMode','manual','YLimMode','manual');
%       if length(arg1) == 6
%           set(ax,'ZLim',arg1(5:6),'ZLimMode','manual');
%       end
%       if length(arg1) == 4 & ~ishold
%           view(2);
%       elseif length(arg1) == 6
%           if (get(ax,'View') == [0 90] & ~ishold)
%               view(3);
%          end
%       end
%   else
%       error('Vector must have 4 or 6 elements.');
%   end
else                                                % 入力引数が文字の時
    for i = 1:nargin
        cur_arg = eval(['arg',num2str(i)]);         % Current Argument
        if strcmp(cur_arg(1:min(4,length(cur_arg))),'auto') % handle AUTO, AUTO[XYZ]: ⇒ 'auto'の時、Z軸のみAutoScalingする
            do_z = (length(cur_arg) == length('auto'));
            if do_z
                set(ax,'ZLimMode','auto');
            else
                set(ax,'ZLimMode','manual');
            end
            set(ax,'XLimMode','manual');
            set(ax,'YLimMode','manual');
%           do_all = (length(cur_arg) == length('auto'));
%           do_x = length(find(cur_arg == 'x'));
%           do_y = length(find(cur_arg == 'y'));
%           do_z = length(find(cur_arg == 'z'));
%           if(do_all | do_x)
%               set(ax,'XLimMode','auto');
%           else
%               set(ax,'XLimMode','manual');
%           end
%           if(do_all | do_y)
%               set(ax,'YLimMode','auto');
%           else
%               set(ax,'YLimMode','manual');
%           end
%           if(do_all | do_z)
%               set(ax,'ZLimMode','auto');
%           else
%               set(ax,'ZLimMode','manual');
%           end
        elseif(strcmp(cur_arg,'tight'))             % handle TIGHT: ⇒ tight scalingの時
            ch = get(ax,'children');
            limits = [inf -inf inf -inf inf -inf];
            hasdepth = 0;
            for i = 1:length(ch),
                switch get(ch(i),'type')
                case {'patch','surface','line'}
                    data{i} = get(ch(i),{'xdata','ydata','zdata'});
                case 'image'
                    data{i} = get(ch(i),{'xdata','ydata','cdata'});
                    siz = size(data{i}{3});         % Offset data limits by half a pixel
                    data{i}{1} = [min(data{i}{1}) max(data{i}{1})];
                    data{i}{2} = [min(data{i}{2}) max(data{i}{2})];
                    dx = diff(data{i}{1}); dy = diff(data{i}{2});
                    data{i}{1} = data{i}{1}   [-dx dx]/(siz(2)-1)/2;
                    data{i}{2} = data{i}{2}   [-dy dy]/(siz(1)-1)/2;
                    data{i}{3} = [];
                case 'text'                         % Ignore text like the axes object does. This handles filtering out the xlabel, ylabel, zlabel, and title objects as well.
                    data{i} = cell(1,3);
                otherwise
                    data{i} = cell(1,3);            % 空のセル配列を作る
                end
                if ~isempty(data{i}{1})
                    limits(1:2) = [min(limits(1),min(data{i}{1}(:))) max(limits(2),max(data{i}{1}(:)))];
                end
                if ~isempty(data{i}{2})
                    limits(3:4) = [min(limits(3),min(data{i}{2}(:))) max(limits(4),max(data{i}{2}(:)))];
                end
                if ~isempty(data{i}{3}),
                    limits(5:6) = [min(limits(5),min(data{i}{3}(:))) max(limits(6),max(data{i}{3}(:)))];
                    if limits(5)~=limits(6)
                        hasdepth = 1;
                    end
                end
            end
            ndx = find(diff(limits) == 0 & [1 0 1 0 1]);    % Protect against axis limit values being the same
            if ~isempty(ndx)
                limits(ndx 1) = limits(ndx) 1;
                limits(ndx) = limits(ndx)-1;
            end
            if hasdepth                             % ここもzaxis特有の設定行である
                set(ax,'ZLim',limits(5:6))
            end
%           if all(isfinite(limits(1:4)))
%               set(ax,'XLim',limits(1:2),'YLim',limits(3:4))
%           end
%           if hasdepth
%               set(ax,'ZLim',limits(5:6))
%           end
        elseif (strcmp(cur_arg, 'fill'))            % handle FILL:
            if strcmp(get(ax,'PlotBoxAspectRatioMode'),'manual') | strcmp(get(ax,'DataAspectRatioMode'),'manual')
                if all(rem(get(ax,'view'),90) ~= 0) % Check for 3-D plot
                    a = axis;
                    axis auto
                    axis image
                    pbar = get(ax,'PlotBoxAspectRatio');
                    if pbar(1) ~= pbarlimit,    set(ax,'XLim',a(1:2));  end
                    if pbar(2) ~= pbarlimit,    set(ax,'YLim',a(3:4));  end
                    if pbar(3) ~= pbarlimit,    set(ax,'ZLim',a(5:6));  end
                    return
                end
                units = get(ax,'Units');
                set(ax,'Units','Pixels')
                a = get(ax,'Position');
                set(ax,'Units',units)               % Change the unconstrained axis limit to 'auto'
                set(ax,'PlotBoxAspectRatio',a([3 4 4])) % based on the axis position.  Also set the pbar.
                if a(3) > a(4)
                    set(ax,'XLimMode','auto')
                else
                    set(ax,'YLimMode','auto')
                end
            end
        elseif (strcmp(cur_arg, 'manual'))          % handle MANUAL:
            set(ax,'ZLimMode','manual');
%           set(ax,'XLimMode','manual','YLimMode','manual','ZLimMode','manual');
        elseif (strcmp(cur_arg, 'ij'))              % handle IJ:
            set(ax,'XDir','normal');
            set(ax,'YDir','reverse');
        elseif (strcmp(cur_arg, 'xy'))              % handle XY:
            set(ax,'XDir','normal');
            set(ax,'YDir','normal');
        elseif (strcmp(cur_arg, 'square'))          % handle SQUARE:
            set(ax,'PlotBoxAspectRatio',[1 1 1],'DataAspectRatioMode','auto')
        elseif (strcmp(cur_arg, 'equal'))           % handle EQUAL:
            if all(rem(get(ax,'view'),90) ~= 0)     % Check for 3-D plot.  If so, use AXIS IMAGE.
                axis image
                return
            end
            units = get(ax,'Units');
            set(ax,'Units','Pixels')
            a = get(ax,'Position');
            set(ax,'Units',units)
            set(ax,'DataAspectRatio',[1 1 1]);
            dx = diff(get(ax,'XLim'));
            dy = diff(get(ax,'YLim'));
            dz = diff(get(ax,'ZLim'));
            set(ax,'PlotBoxAspectRatio', [a(3) a(4) dz*min(a(3),a(4))/min(dx,dy)]);
            if a(3) < a(4)                          % Change the unconstrained axis limit to auto based on the PBAR.
                set(ax,'XLimMode','auto')
            else
                set(ax,'YLimMode','auto')
            end
        elseif (strcmp(cur_arg,'image'))            % handle IMAGE:
            set(ax,'DataAspectRatio',[1 1 1], 'PlotBoxAspectRatioMode','auto')
            pbar = get(ax,'PlotBoxAspectRatio');    % Limit plotbox aspect ratio to 1 to 25 ratio.
            pbar = max(pbarlimit,pbar / max(pbar));
            if any(pbar(1:2) == pbarlimit)
                set(ax,'PlotBoxAspectRatio',pbar)
            end
            axis tight
        elseif (strcmp(cur_arg, 'normal'))          % handle NORMAL:
            set(ax,'PlotBoxAspectRatioMode','auto','DataAspectRatioMode','auto')
            set(ax,'CameraViewAngleMode','auto');
        elseif (strcmp(cur_arg, 'off'))             % handle OFF:
            set(ax,'Visible','off');
            set(get(ax,'Title'),'Visible','on');
        elseif (strcmp(cur_arg, 'on'))              % handle ON:
            set(ax,'Visible','on');
        elseif (strcmp(cur_arg,'vis3d'))            % handle VIS3D:
            set(ax,'CameraViewAngleMode','manual');
            set(ax,'DataAspectRatioMode','manual');
            set(ax,'PlotBoxAspectRatioMode','manual');
        elseif (strcmp(cur_arg, 'state'))           % handle STATE:
            warning(sprintf(['AXIS(''STATE'') is obsolete and will be eliminated\n         in future versions. Use GET(GCA,...) instead.']));
            str = '';
%           if (strcmp(get(ax,'XLimMode'), 'auto'))
%               str = 'x';
%           end
%           if (strcmp(get(ax,'YLimMode'), 'auto'))
%               str = [str, 'y'];
%           end
            if (strcmp(get(ax,'ZLimMode'), 'auto'))
                str = 'z';
            end
            if isempty(str)
                ans1 = 'manual';
            else
                ans1=  'auto';
%           if length(str) == 3
%               ans1 = 'auto';
%           else
%               ans1 = 'manual';
            end
            if(nargout > 1)
                if strcmp(get(ax,'Visible'),'on')
                    ans2 = 'on';
                else
                    ans2 = 'off';
                end
            end
            if (nargout > 2)
                ans3 = 'xy';
                if (strcmp(get(ax,'XDir'),'normal') & strcmp(get(ax,'YDir'),'reverse'))
                    ans3 = 'ij';
                end
            end
        else                                        % handle ERROR (NONE OF THE ABOVE STRINGS FOUND):
            error(['Unknown command option ''',cur_arg,'''']);
        end
    end
end