Home > FMAToolbox > Plot > AdjustAxes.m

AdjustAxes

PURPOSE ^

AdjustAxes - Adjust axes limits for all subplots

SYNOPSIS ^

function AdjustAxes(f,varargin)

DESCRIPTION ^

AdjustAxes - Adjust axes limits for all subplots

  USAGE

    AdjustAxes(f,<options>)

    f              optional figure handle (default = gcf)
    <options>      optional list of property-value pairs (see table below)

    =========================================================================
     Properties    Values
    -------------------------------------------------------------------------
     'x'           [min max], 'uniform' (same for all axes) or 'auto'
     'y'           [min max], 'uniform' (same for all axes) or 'auto'
     'xy'          [min max], 'uniform' (same for all axes) or 'auto'
    =========================================================================

  DEFAULTS

    When no option is provided, all axes are automatically adjusted (same as
    xlim('xy','uniform')). When 'x' is provided alone, y axes are left unchanged
    and vice versa.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function AdjustAxes(f,varargin)
0002 
0003 %AdjustAxes - Adjust axes limits for all subplots
0004 %
0005 %  USAGE
0006 %
0007 %    AdjustAxes(f,<options>)
0008 %
0009 %    f              optional figure handle (default = gcf)
0010 %    <options>      optional list of property-value pairs (see table below)
0011 %
0012 %    =========================================================================
0013 %     Properties    Values
0014 %    -------------------------------------------------------------------------
0015 %     'x'           [min max], 'uniform' (same for all axes) or 'auto'
0016 %     'y'           [min max], 'uniform' (same for all axes) or 'auto'
0017 %     'xy'          [min max], 'uniform' (same for all axes) or 'auto'
0018 %    =========================================================================
0019 %
0020 %  DEFAULTS
0021 %
0022 %    When no option is provided, all axes are automatically adjusted (same as
0023 %    xlim('xy','uniform')). When 'x' is provided alone, y axes are left unchanged
0024 %    and vice versa.
0025 %
0026 
0027 % Copyright (C) 2012 by Michaƫl Zugaro
0028 %
0029 % This program is free software; you can redistribute it and/or modify
0030 % it under the terms of the GNU General Public License as published by
0031 % the Free Software Foundation; either version 3 of the License, or
0032 % (at your option) any later version.
0033 
0034 % Default values
0035 xlims = [];
0036 ylims = [];
0037 
0038 % Optional parameter
0039 if nargin < 1,
0040     f = gcf;
0041 elseif ischar(f),
0042     varargin = {f,varargin{:}};
0043     f = gcf;
0044 elseif ~ishandle(f),
0045     error('Incorrect value for ''f'' (type ''help <a href="matlab:help AdjustAxes">AdjustAxes</a>'' for details).');
0046 end
0047 
0048 % Parse parameter list
0049 for i = 1:2:length(varargin),
0050   if ~ischar(varargin{i}),
0051     error(['Parameter ' num2str(i+1) ' is not a property (type ''help <a href="matlab:help AdjustAxes">AdjustAxes</a>'' for details).']);
0052   end
0053   switch(lower(varargin{i})),
0054     case 'x',
0055       xlims = lower(varargin{i+1});
0056       if ~isdvector(xlims,'<') && ~isastring(xlims,'auto','uniform'),
0057         error('Incorrect value for property ''xlims'' (type ''help <a href="matlab:help AdjustAxes">AdjustAxes</a>'' for details).');
0058       end
0059     case 'y',
0060       ylims = lower(varargin{i+1});
0061       if ~isdvector(ylims,'<') && ~isastring(ylims,'auto','uniform'),
0062         error('Incorrect value for property ''ylims'' (type ''help <a href="matlab:help AdjustAxes">AdjustAxes</a>'' for details).');
0063       end
0064     case 'xy',
0065       xlims = lower(varargin{i+1});
0066       ylims = lower(varargin{i+1});
0067       if ~isdvector(xlims,'<') && ~isastring(xlims,'auto','uniform'),
0068         error('Incorrect value for property ''AdjustAxess'' (type ''help <a href="matlab:help AdjustAxes">AdjustAxes</a>'' for details).');
0069       end
0070     otherwise,
0071       error(['Unknown property ''' num2str(varargin{i}) ''' (type ''help <a href="matlab:help AdjustAxes">AdjustAxes</a>'' for details).']);
0072   end
0073 end
0074 
0075 if isempty(xlims) && isempty(ylims),
0076     xlims = 'uniform';
0077     ylims = 'uniform';
0078 end
0079 
0080 % Compute min and max limits for all subplots
0081 sub = get(f,'children');
0082 if strcmp(xlims,'uniform'),
0083     lims = [];
0084     for i = 1:length(sub),
0085         if strcmp(get(sub,'type'),'axes'),
0086             lims(end+1,:) = xlim(sub(i));
0087         end
0088     end
0089     if isempty(lims), return; end
0090     xlims = [min(lims(:,1)) max(lims(:,2))];
0091 end
0092 if strcmp(ylims,'uniform'),
0093     lims = [];
0094     for i = 1:length(sub),
0095         if strcmp(get(sub,'type'),'axes'),
0096             lims(end+1,:) = ylim(sub(i));
0097         end
0098     end
0099     if isempty(lims), return; end
0100     ylims = [min(lims(:,1)) max(lims(:,2))];
0101 end
0102 
0103 % Apply
0104 for i = 1:length(sub),
0105     if strcmp(get(sub,'type'),'axes'),
0106         if strcmp(xlims,'auto'),
0107             xlim(sub(i),'auto');
0108         elseif ~isempty(xlims),
0109             xlim(sub(i),xlims);
0110         end
0111         if strcmp(ylims,'auto'),
0112             ylim(sub(i),'auto');
0113         elseif ~isempty(ylims),
0114             ylim(sub(i),ylims);
0115         end
0116     end
0117 end

Generated on Fri 16-Mar-2018 13:00:20 by m2html © 2005