Home > FMAToolbox > Plot > SideAxes.m

SideAxes

PURPOSE ^

SideAxes - Add side axes to existing axes.

SYNOPSIS ^

function h = SideAxes(a,location,s,varargin)

DESCRIPTION ^

SideAxes - Add side axes to existing axes.

  USAGE

    h = SideAxes(a,location,s,<options>)

    Using cell arrays will overlay variable pairs.

    a              optional target axes (default = gca)
    location       'top', 'bottom', 'left' or 'right'
    s              size, as proportion of target axes
    <options>      optional list of property-value pairs (see table below)

    =========================================================================
     Properties    Values
    -------------------------------------------------------------------------
     'gap'         gap between target and side axes, as proportion of total
                   width or height (default = 0.1)
    =========================================================================

  OUTPUT

    h              handle to the new axes

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function h = SideAxes(a,location,s,varargin)
0002 
0003 %SideAxes - Add side axes to existing axes.
0004 %
0005 %  USAGE
0006 %
0007 %    h = SideAxes(a,location,s,<options>)
0008 %
0009 %    Using cell arrays will overlay variable pairs.
0010 %
0011 %    a              optional target axes (default = gca)
0012 %    location       'top', 'bottom', 'left' or 'right'
0013 %    s              size, as proportion of target axes
0014 %    <options>      optional list of property-value pairs (see table below)
0015 %
0016 %    =========================================================================
0017 %     Properties    Values
0018 %    -------------------------------------------------------------------------
0019 %     'gap'         gap between target and side axes, as proportion of total
0020 %                   width or height (default = 0.1)
0021 %    =========================================================================
0022 %
0023 %  OUTPUT
0024 %
0025 %    h              handle to the new axes
0026 
0027 
0028 % Copyright (C) 2013-2015 by Michaƫl Zugaro
0029 %
0030 % This program is free software; you can redistribute it and/or modify
0031 % it under the terms of the GNU General Public License as published by
0032 % the Free Software Foundation; either version 3 of the License, or
0033 % (at your option) any later version.
0034 
0035 % Defaults
0036 gap = 0.1;
0037 
0038 % Check number of parameters
0039 if nargin < 2,
0040     error('Incorrect number of parameters (type ''help <a href="matlab:help SideAxes">SideAxes</a>'' for details).');
0041 end
0042 
0043 % Optional axes
0044 if nargin == 2,
0045     s = location;
0046     location = a;
0047     a = gca;
0048 elseif isastring(a),
0049     varargin = {s,varargin{:}};
0050     s = location;
0051     location = a;
0052     a = gca;
0053 end
0054 
0055 % Check number of options
0056 if mod(length(varargin),2) ~= 0,
0057     error('Incorrect number of parameters (type ''help <a href="matlab:help SideAxes">SideAxes</a>'' for details).');
0058 end
0059 
0060 % Check parameters
0061 if ~ishandle(a),
0062     error('Incorrect axes (type ''help <a href="matlab:help SideAxes">SideAxes</a>'' for details).');
0063 end
0064 if ~isdscalar(s,'>0','<1'),
0065     error('Incorrect size (type ''help <a href="matlab:help SideAxes">SideAxes</a>'' for details).');
0066 end
0067 location = lower(location);
0068 if ~isastring(location,'top','bottom','left','right'),
0069     error('Incorrect location (type ''help <a href="matlab:help SideAxes">SideAxes</a>'' for details).');
0070 end
0071 
0072 % Parse parameter list
0073 for i = 1:2:length(varargin),
0074     if ~ischar(varargin{i}),
0075         error(['Parameter ' num2str(i+2) ' is not a property (type ''help <a href="matlab:help SideAxes">SideAxes</a>'' for details).']);
0076     end
0077     switch(lower(varargin{i})),
0078         case 'gap',
0079             gap = varargin{i+1};
0080             if ~isdscalar(gap,'>=0','<1'),
0081                 error('Incorrect value for property ''gap'' (type ''help <a href="matlab:help SideAxes">SideAxes</a>'' for details).');
0082             end
0083         otherwise,
0084             error(['Unknown property ''' num2str(varargin{i}) ''' (type ''help <a href="matlab:help SideAxes">SideAxes</a>'' for details).']);
0085     end
0086 end
0087 
0088 % Resize target axes to make room for side axes
0089 
0090 main = a;
0091 position = get(main,'position');
0092 x = position(1);
0093 y = position(2);
0094 currentWidth = position(3); % current width
0095 currentHeight = position(4); % current height
0096 
0097 % The following variables will be used only when adding left or right side axes
0098 horizontalGap = currentWidth * gap;
0099 newWidth = currentWidth - horizontalGap;
0100 newMainWidth = newWidth * (1-s);
0101 newSideWidth = newWidth * s;
0102 
0103 % The following variables will be used only when adding left or right side axes
0104 verticalGap = currentHeight * gap;
0105 newHeight = currentHeight - verticalGap;
0106 newMainHeight = newHeight * (1-s);
0107 newSideHeight = newHeight * s;
0108 
0109 xLims = get(main,'xlim');
0110 yLims = get(main,'ylim');
0111 
0112 switch(location),
0113     case 'top',
0114         set(main,'position',[x y currentWidth newMainHeight]);
0115         h = axes('position',[x y+newMainHeight+verticalGap currentWidth newSideHeight]);
0116         xlim(h,xLims);
0117     case 'bottom',
0118         set(main,'position',[x y+newSideHeight+verticalGap currentWidth newMainHeight]);
0119         h = axes('position',[x y currentWidth newSideHeight]);
0120         xlim(h,xLims);
0121     case 'right',
0122         set(main,'position',[x y newMainWidth currentHeight]);
0123         h = axes('position',[x+newMainWidth+horizontalGap y newSideWidth currentHeight]);
0124         ylim(h,yLims);
0125     case 'left',
0126         set(main,'position',[x+newSideWidth+horizontalGap y newMainWidth currentHeight]);
0127         h = axes('position',[x y newSideWidth currentHeight]);
0128         ylim(h,yLims);
0129 end

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