ファイル名:ZaxisAll.m

function [ans1, ans2, ans3] = zaxisall(arg1, arg2, arg3, arg4);
% ZAXISALL  Control zaxis scalings and appearances.
%   TIGHT   set(gca,'Xlim','Ylim')のとこだけ変えていて範囲の計算はXYZ軸全てで計算している。
%   FILL    何もしていない。
%
%   ZAXISALL([XMIN XMAX])  sets scaling for the z-axes on the current plot.
%   V = ZAXISALL  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.
%   ZAXISALL 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.
%   ZAXISALL MANUAL  freezes the zaxis scaling at the current limits, so that if HOLD is turned on,
%       subsequent plots will use the same limits.
%   ZAXISALL TIGHT  sets the zaxis limits to the range of the data.
%   ZAXISALL 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.
%   ZAXISALL 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.
%   ZAXISALL 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.
%   ZAXISALL 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.
%   ZAXISALL IMAGE  is the same as ZAXISALL EQUAL except that the plot box fits tightly around
%       the data.
%   ZAXISALL SQUARE  makes the current axis box square in size.
%   ZAXISALL NORMAL  restores the current axis box to full size and removes any restrictions on
%       the scaling of the units. This undoes the effects of ZAXISALL SQUARE and ZAXISALL EQUAL.
%   ZAXISALL OFF  turns off all axis labeling, tick marks and background.
%   ZAXISALL 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;
ax = flipud(findobj(gcf,'Type','axes'));
pbarlimit = 0.1;
if (nargin == 0)                                    % 入力引数がないときは現在の軸の範囲を返す
    for k = 1:size(ax,1)                            % zaxisallだけは常にZ軸が表示されるようにする
        ans1(k,:) = [get(ax(k,1),'XLim') get(ax(k,1),'YLim') get(ax(k,1),'ZLim')];
    end
%   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個でそれが文字でない時は[ymin ymax]だと判断する
    if (length(arg1) == 2)
        set(ax,'ZLim',arg1(1:2),'ZLimMode','manual');
        for k = 1:size(ax,1)
            set(gcf,'CurrentAxes',ax(k,1));
            if (get(ax(k,1),'View') == [0 90] & ~ishold)    % Hold onされてなかったら強制的に2次元の絵にする
                view(3)                             % axisallでは2-3次元の混在も考えられるのでこの行は除いても良い
            end                                     % ここもzaxis特有の設定行である
        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');
            for k = 1:size(ax,1)                    % それぞれのAxes軸で計算する
                limits = [inf -inf inf -inf inf -inf];
                hasdepth = 0;
                for i = 1:length(ch{k,1}),          % それぞれのAxesの子オブジェクトについて計算する
%               for i = 1:length(ch)
                    switch get(ch{k,1}(i),'type')
%                   switch get(ch(i),'type')
                    case {'patch','surface','line'}
                        data{i} = get(ch{k,1}(i),{'xdata','ydata','zdata'});
%                       data{i} = get(ch(i),{'xdata','ydata','zdata'});
                    case 'image'
                        data{i} = get(ch{k,1}(i),{'xdata','ydata','cdata'});
%                       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
                    set(ax(k,1),'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
                clear data
            end
        elseif strcmp(cur_arg, 'fill')              % handle FILL
            for k = 1:size(ax,1)
                if strcmp(get(ax(k,1),'PlotBoxAspectRatioMode'),'manual') | strcmp(get(ax(k,1),'DataAspectRatioMode'),'manual')
                    if all(rem(get(ax(k,1),'view'),90) ~= 0)    % Check for 3-D plot
%                   if all(rem(get(ax,'view'),90) ~= 0)         % Check for 3-D plot
                        set(gcf,'CurrentAxes',ax(k,1));
                        a = axis;
                        axis auto
                        axis image
                        pbar = get(ax(k,1),'PlotBoxAspectRatio');
                        if pbar(1) ~= pbarlimit,    set(ax(k,1),'XLim',a(1:2));     end
                        if pbar(2) ~= pbarlimit,    set(ax(k,1),'YLim',a(3:4));     end
                        if pbar(3) ~= pbarlimit,    set(ax(k,1),'ZLim',a(5:6));     end
%                       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
                    else
                        units = get(ax(k,1),'Units');
                        set(ax(k,1),'Units','Pixels')
                        a = get(ax(k,1),'Position');
                        set(ax(k,1),'Units',units)                      % Change the unconstrained axis limit to 'auto'
                        set(ax(k,1),'PlotBoxAspectRatio',a([3 4 4]))    % based on the axis position.  Also set the pbar.
                        if a(3) > a(4), set(ax(k,1),'XLimMode','auto')
                        else            set(ax(k,1),'YLimMode','auto')
                        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
                end
            end
        elseif strcmp(cur_arg, 'manual')            % handle MANUAL
            set(ax,'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
            for k = 1:size(ax,1)
                if all(rem(get(ax(k,1),'view'),90) ~= 0)    % Check for 3-D plot.  If so, use AXIS IMAGE.
%               if all(rem(get(ax,'view'),90) ~= 0)         % Check for 3-D plot.  If so, use AXIS IMAGE.
                    set(gcf,'CurrentAxes',ax(k,1))
                    axis image
%                   return
%               end
                else
                    units = get(ax(k,1),'Units');
                    set(ax(k,1),'Units','Pixels')
                    a = get(ax(k,1),'Position');
                    set(ax(k,1),'Units',units)
                    set(ax(k,1),'DataAspectRatio',[1 1 1]);
                    dx = diff(get(ax(k,1),'XLim'));
                    dy = diff(get(ax(k,1),'YLim'));
                    dz = diff(get(ax(k,1),'ZLim'));
                    set(ax(k,1),'PlotBoxAspectRatio',[a(3) a(4) dz*min(a(3),a(4))/min(dx,dy)]);
%                   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(k,1),'XLimMode','auto')
%                       set(ax,'XLimMode','auto')
                    else
                        set(ax(k,1),'YLimMode','auto')
%                       set(ax,'YLimMode','auto')
                    end
                end
            end
        elseif strcmp(cur_arg,'image')              % handle IMAGE
            set(ax,'DataAspectRatio',[1 1 1],'PlotBoxAspectRatioMode','auto')
            for k = 1:size(ax,1)
                pbar = get(ax(k,1),'PlotBoxAspectRatio');   % Limit plotbox aspect ratio to 1 to 25 ratio.
%               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(k,1),'PlotBoxAspectRatio',pbar);     end
%               if any(pbar(1:2) == pbarlimit), set(ax,'PlotBoxAspectRatio',pbar);          end
                set(gcf,'CurrentAxes',ax(k,1));
                axis tight
            end
        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');
            for k = 1:size(ax,1)
                set(get(ax(k,1),'Title'),'Visible','on');
            end
%           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.']));
            for k = 1:size(ax,1)
                 str = '';
                if(strcmp(get(ax(k,1),'ZLimMode'), 'auto')),    str = 'z';          end
%               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 = [str, 'z'];   end
                if isempty(str),        ans1{k,1} = 'manual';
                else                    ans1{k,1} = 'auto';
%               if length(str) == 3     ans1 = 'auto';
%               else                    ans1 = 'manual';
                end
                if nargout > 1
                    if strcmp(get(ax(k,1),'Visible'),'on'), ans2{k,1} = 'on';
                    else                                    ans2{k,1} = 'off';
%                   if strcmp(get(ax,'Visible'),'on'),      ans2 = 'on';
%                   else                                    ans2 = 'off';
                    end
                end
                if nargout > 2
                    ans3{k,1} = 'xy';
%                   ans3 = 'xy';
                    if strcmp(get(ax(k,1),'XDir'),'normal') & strcmp(get(ax(k,1),'YDir'),'reverse')
                        ans3{k,1} = 'ij';
%                   if strcmp(get(ax,'XDir'),'normal') & strcmp(get(ax,'YDir'),'reverse')
%                       ans3 = 'ij';
                    end
                end
            end
        else                                        % handle ERROR (NONE OF THE ABOVE STRINGS FOUND)
            error(['Unknown command option ''',cur_arg,'''']);
        end
    end
end