0001 function h=vb_subaxis(varargin)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
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
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
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
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
0103
0104
0105
0106
0107
0108
0109
0110
0111
0112
0113
0114
0115
0116
0117
0118
0119
0120
0121
0122
0123
0124
0125
0126
0127
0128
0129
0130
0131
0132
0133
0134
0135
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
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
0162 Fnames=fieldnames(ArgStruct);
0163 for i=1:length(Fnames)
0164 name=lower(Fnames{i,1});
0165 Fnames{i,2}=name;
0166 AbbrevIdx=find(Fnames{i,1}~=name);
0167 Fnames{i,3}=[name(AbbrevIdx) ' '];
0168
0169 Fnames{i,4}=isempty(strmatch(Fnames{i,2},FlagTypeParams));
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');
0178 if isempty(FieldIdx)
0179 FieldIdx=strmatch(name,FnamesFull);
0180 end
0181 Aliases{i,2}=FieldIdx;
0182 AbbrevIdx=find(Aliases{i,1}~=name);
0183 Aliases{i,3}=[name(AbbrevIdx) ' '];
0184 Aliases{i,1}=name;
0185 end
0186
0187 FnamesFull=strvcat(FnamesFull,strvcat(Aliases{:,1}));
0188 FnamesAbbr=strvcat(FnamesAbbr,strvcat(Aliases{:,3}));
0189 end
0190
0191
0192 l=NumArgCount+1;
0193 while (l<=length(args))
0194 a=args{l};
0195 if ischar(a)
0196 paramHasValue=1;
0197 a=lower(a);
0198 FieldIdx=strmatch(a,FnamesAbbr,'exact');
0199 if isempty(FieldIdx)
0200 FieldIdx=strmatch(a,FnamesFull);
0201 end
0202 if (length(FieldIdx)>1)
0203 [mx,mxi]=max(sum(FnamesFull(FieldIdx,:)==' ',2));
0204 FieldIdx=FieldIdx(mxi);
0205 end
0206 if FieldIdx>length(Fnames)
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'
0214 if (Fnames{curField,4})
0215 val=args{l+1};
0216 else
0217 val=1;
0218 end
0219 ArgStruct.(Fnames{curField,1})=val;
0220 end
0221 l=l+1+Fnames{FieldIdx(1),4};
0222 else
0223 error(['Expected a named parameter: ' num2str(a)])
0224 end
0225 end
0226
0227