Home > vbmeg > functions > common > utility > vb_subaxis.m

vb_subaxis

PURPOSE ^

SUBAXIS Create axes in tiled positions. (just like subplot)

SYNOPSIS ^

function h=vb_subaxis(varargin)

DESCRIPTION ^

SUBAXIS Create axes in tiled positions. (just like subplot)
   Usage:
      h=vb_subaxis(rows,cols,cellno[,settings])
      h=vb_subaxis(rows,cols,cellx,celly[,settings])
      h=vb_subaxis(rows,cols,cellx,celly,spanx,spany[,settings])

 SETTINGS: Spacing,SpacingHoriz,SpacingVert
           Padding,PaddingRight,PaddingLeft,PaddingTop,PaddingBottom
           Margin,MarginRight,MarginLeft,MarginTop,MarginBottom
           Holdaxis

           all units are relative (e.g from 0 to 1)

           Abbreviations of parameters can be used.. (Eg MR instead of MarginRight)
           (holdaxis means that it wont delete any axes below.)


 Example:

   >> vb_subaxis(2,1,1,'SpacingVert',0,'MR',0); 
   >> imagesc(magic(3))
   >> vb_subaxis(2,'p',.02);
   >> imagesc(magic(4))

 2001 / Aslak Grinsted  (Feel free to modify this code.)

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function h=vb_subaxis(varargin)
0002 %SUBAXIS Create axes in tiled positions. (just like subplot)
0003 %   Usage:
0004 %      h=vb_subaxis(rows,cols,cellno[,settings])
0005 %      h=vb_subaxis(rows,cols,cellx,celly[,settings])
0006 %      h=vb_subaxis(rows,cols,cellx,celly,spanx,spany[,settings])
0007 %
0008 % SETTINGS: Spacing,SpacingHoriz,SpacingVert
0009 %           Padding,PaddingRight,PaddingLeft,PaddingTop,PaddingBottom
0010 %           Margin,MarginRight,MarginLeft,MarginTop,MarginBottom
0011 %           Holdaxis
0012 %
0013 %           all units are relative (e.g from 0 to 1)
0014 %
0015 %           Abbreviations of parameters can be used.. (Eg MR instead of MarginRight)
0016 %           (holdaxis means that it wont delete any axes below.)
0017 %
0018 %
0019 % Example:
0020 %
0021 %   >> vb_subaxis(2,1,1,'SpacingVert',0,'MR',0);
0022 %   >> imagesc(magic(3))
0023 %   >> vb_subaxis(2,'p',.02);
0024 %   >> imagesc(magic(4))
0025 %
0026 % 2001 / Aslak Grinsted  (Feel free to modify this code.)
0027 f=gcf;
0028 
0029 
0030 Args=[];
0031 UserDataArgsOK=0;
0032 Args=get(f,'UserData');
0033 if isstruct(Args) 
0034     UserDataArgsOK=isfield(Args,'SpacingHorizontal')&isfield(Args,'Holdaxis')&isfield(Args,'rows')&isfield(Args,'cols');
0035 end
0036 OKToStoreArgs=isempty(Args)|UserDataArgsOK;
0037 
0038 if isempty(Args)&(~UserDataArgsOK)
0039     Args=struct('Holdaxis',0, ...
0040         'SpacingVertical',0.05,'SpacingHorizontal',0.05, ...
0041         'PaddingLeft',0,'PaddingRight',0,'PaddingTop',0,'PaddingBottom',0, ...
0042         'MarginLeft',.1,'MarginRight',.1,'MarginTop',.1,'MarginBottom',.1, ...
0043         'rows',[],'cols',[]); 
0044 end
0045 Args=parseArgs(varargin,Args,{'Holdaxis'},{'Spacing' {'sh','sv'}; 'Padding' {'pl','pr','pt','pb'}; 'Margin' {'ml','mr','mt','mb'}});
0046 
0047 if (length(Args.NumericArguments)>1)
0048     Args.rows=Args.NumericArguments{1};
0049     Args.cols=Args.NumericArguments{2};
0050 %remove these 2 numerical arguments
0051     Args.NumericArguments={Args.NumericArguments{3:end}};
0052 end
0053 
0054 if OKToStoreArgs
0055     set(f,'UserData',Args);
0056 end
0057 
0058 
0059     
0060 
0061 switch length(Args.NumericArguments)
0062    case 0
0063        return % no arguments but rows/cols....
0064    case 1
0065       x1=mod((Args.NumericArguments{1}-1),Args.cols)+1; x2=x1;
0066       y1=floor((Args.NumericArguments{1}-1)/Args.cols)+1; y2=y1;
0067    case 2
0068       x1=Args.NumericArguments{1};x2=x1;
0069       y1=Args.NumericArguments{2};y2=y1;
0070    case 4
0071       x1=Args.NumericArguments{1};x2=x1+Args.NumericArguments{3}-1;
0072       y1=Args.NumericArguments{2};y2=y1+Args.NumericArguments{4}-1;
0073    otherwise
0074       error('vb_subaxis argument error')
0075 end
0076     
0077 
0078 cellwidth=((1-Args.MarginLeft-Args.MarginRight)-(Args.cols-1)*Args.SpacingHorizontal)/Args.cols;
0079 cellheight=((1-Args.MarginTop-Args.MarginBottom)-(Args.rows-1)*Args.SpacingVertical)/Args.rows;
0080 xpos1=Args.MarginLeft+Args.PaddingLeft+cellwidth*(x1-1)+Args.SpacingHorizontal*(x1-1);
0081 xpos2=Args.MarginLeft-Args.PaddingRight+cellwidth*x2+Args.SpacingHorizontal*(x2-1);
0082 ypos1=Args.MarginTop+Args.PaddingTop+cellheight*(y1-1)+Args.SpacingVertical*(y1-1);
0083 ypos2=Args.MarginTop-Args.PaddingBottom+cellheight*y2+Args.SpacingVertical*(y2-1);
0084 
0085 if Args.Holdaxis
0086     h=axes('position',[xpos1 1-ypos2 xpos2-xpos1 ypos2-ypos1]);
0087 else
0088     h=subplot('position',[xpos1 1-ypos2 xpos2-xpos1 ypos2-ypos1]);
0089 end
0090 
0091 
0092 set(h,'box','on');
0093 %h=axes('position',[x1 1-y2 x2-x1 y2-y1]);
0094 set(h,'units',get(gcf,'defaultaxesunits'));
0095 set(h,'tag','vb_subaxis');
0096 
0097 
0098 
0099 if (nargout==0) clear h; end;
0100 
0101 function ArgStruct=parseArgs(args,ArgStruct,varargin)
0102 % Helper function for parsing varargin.
0103 %
0104 %
0105 % ArgStruct=parseArgs(varargin,ArgStruct[,FlagtypeParams[,Aliases]])
0106 %
0107 % * ArgStruct is the structure full of named arguments with default values.
0108 % * Flagtype params is params that don't require a value. (the value will be set to 1 if it is present)
0109 % * Aliases can be used to map one argument-name to several argstruct fields
0110 %
0111 %
0112 % example usage:
0113 % --------------
0114 % function parseargtest(varargin)
0115 %
0116 % %define the acceptable named arguments and assign default values
0117 % Args=struct('Holdaxis',0, ...
0118 %        'SpacingVertical',0.05,'SpacingHorizontal',0.05, ...
0119 %        'PaddingLeft',0,'PaddingRight',0,'PaddingTop',0,'PaddingBottom',0, ...
0120 %        'MarginLeft',.1,'MarginRight',.1,'MarginTop',.1,'MarginBottom',.1, ...
0121 %        'rows',[],'cols',[]);
0122 %
0123 % %The capital letters define abrreviations.
0124 % %  Eg. parseargtest('spacingvertical',0) is equivalent to  parseargtest('sv',0)
0125 %
0126 % Args=parseArgs(varargin,Args, ... % fill the arg-struct with values entered by the user
0127 %           {'Holdaxis'}, ... %this argument has no value (flag-type)
0128 %           {'Spacing' {'sh','sv'}; 'Padding' {'pl','pr','pt','pb'}; 'Margin' {'ml','mr','mt','mb'}});
0129 %
0130 % disp(Args)
0131 %
0132 %
0133 %
0134 %
0135 % % Aslak Grinsted 2003
0136 
0137 Aliases={};
0138 FlagTypeParams='';
0139 
0140 if (length(varargin)>0) 
0141     FlagTypeParams=strvcat(varargin{1});
0142     if length(varargin)>1
0143         Aliases=varargin{2};
0144     end
0145 end
0146  
0147 
0148 %---------------Get "numeric" arguments
0149 NumArgCount=1;
0150 while (NumArgCount<=size(args,2))&(~ischar(args{NumArgCount}))
0151     NumArgCount=NumArgCount+1;
0152 end
0153 NumArgCount=NumArgCount-1;
0154 if (NumArgCount>0)
0155     ArgStruct.NumericArguments={args{1:NumArgCount}};
0156 else
0157     ArgStruct.NumericArguments={};
0158 end 
0159 
0160 
0161 %--------------Make an accepted fieldname matrix (case insensitive)
0162 Fnames=fieldnames(ArgStruct);
0163 for i=1:length(Fnames)
0164     name=lower(Fnames{i,1});
0165     Fnames{i,2}=name; %col2=lower
0166     AbbrevIdx=find(Fnames{i,1}~=name);
0167     Fnames{i,3}=[name(AbbrevIdx) ' ']; %col3=abreviation letters (those that are uppercase in the ArgStruct) e.g. SpacingHoriz->sh
0168     %the space prevents strvcat from removing empty lines
0169     Fnames{i,4}=isempty(strmatch(Fnames{i,2},FlagTypeParams)); %Does this parameter have a value? (e.g. not flagtype)
0170 end
0171 FnamesFull=strvcat(Fnames{:,2});
0172 FnamesAbbr=strvcat(Fnames{:,3});
0173 
0174 if length(Aliases)>0  
0175     for i=1:length(Aliases)
0176         name=lower(Aliases{i,1});
0177         FieldIdx=strmatch(name,FnamesAbbr,'exact'); %try abbreviations (must be exact)
0178         if isempty(FieldIdx) 
0179             FieldIdx=strmatch(name,FnamesFull); %&??????? exact or not?
0180         end
0181         Aliases{i,2}=FieldIdx;
0182         AbbrevIdx=find(Aliases{i,1}~=name);
0183         Aliases{i,3}=[name(AbbrevIdx) ' ']; %the space prevents strvcat from removing empty lines
0184         Aliases{i,1}=name; %dont need the name in uppercase anymore for aliases
0185     end
0186     %Append aliases to the end of FnamesFull and FnamesAbbr
0187     FnamesFull=strvcat(FnamesFull,strvcat(Aliases{:,1})); 
0188     FnamesAbbr=strvcat(FnamesAbbr,strvcat(Aliases{:,3}));
0189 end
0190 
0191 %--------------get parameters--------------------
0192 l=NumArgCount+1; 
0193 while (l<=length(args))
0194     a=args{l};
0195     if ischar(a)
0196         paramHasValue=1; % assume that the parameter has is of type 'param',value
0197         a=lower(a);
0198         FieldIdx=strmatch(a,FnamesAbbr,'exact'); %try abbreviations (must be exact)
0199         if isempty(FieldIdx) 
0200             FieldIdx=strmatch(a,FnamesFull); 
0201         end
0202         if (length(FieldIdx)>1) %shortest fieldname should win
0203             [mx,mxi]=max(sum(FnamesFull(FieldIdx,:)==' ',2));
0204             FieldIdx=FieldIdx(mxi);
0205         end
0206         if FieldIdx>length(Fnames) %then it's an alias type.
0207             FieldIdx=Aliases{FieldIdx-length(Fnames),2}; 
0208         end
0209         
0210         if isempty(FieldIdx) 
0211             error(['Unknown named parameter: ' a])
0212         end
0213         for curField=FieldIdx' %if it is an alias it could be more than one.
0214             if (Fnames{curField,4})
0215                 val=args{l+1};
0216             else
0217                 val=1; %parameter is of flag type and is set (1=true)....
0218             end
0219             ArgStruct.(Fnames{curField,1})=val;
0220         end
0221         l=l+1+Fnames{FieldIdx(1),4}; %if a wildcard matches more than one
0222     else
0223         error(['Expected a named parameter: ' num2str(a)])
0224     end
0225 end
0226 
0227

Generated on Mon 22-May-2023 06:53:56 by m2html © 2005