1 function h=subaxis(varargin)
2 %SUBAXIS Create axes in tiled positions. (just like subplot)
4 % h=subaxis(rows,cols,cellno[,settings])
5 % h=subaxis(rows,cols,cellx,celly[,settings])
6 % h=subaxis(rows,cols,cellx,celly,spanx,spany[,settings])
8 % SETTINGS: Spacing,SpacingHoriz,SpacingVert
9 % Padding,PaddingRight,PaddingLeft,PaddingTop,PaddingBottom
10 % Margin,MarginRight,MarginLeft,MarginTop,MarginBottom
13 % all units are relative (e.g from 0 to 1)
15 % Abbreviations of parameters can be used.. (Eg MR instead of MarginRight)
16 % (holdaxis means that it wont
delete any axes below.)
21 % >> subaxis(2,1,1,
'SpacingVert',0,
'MR',0);
22 % >> imagesc(magic(3))
23 % >> subaxis(2,
'p',.02);
24 % >> imagesc(magic(4))
26 % 2001 / Aslak Grinsted (Feel free to modify
this code.)
32 Args=
get(f,
'UserData');
34 UserDataArgsOK=isfield(Args,'SpacingHorizontal')&isfield(Args,'Holdaxis')&isfield(Args,'rows')&isfield(Args,'cols');
36 OKToStoreArgs=isempty(Args)|UserDataArgsOK;
38 if isempty(Args)&(~UserDataArgsOK)
39 Args=struct('Holdaxis',0, ...
40 'SpacingVertical',0.05,'SpacingHorizontal',0.05, ...
41 'PaddingLeft',0,'PaddingRight',0,'PaddingTop',0,'PaddingBottom',0, ...
42 'MarginLeft',.1,'MarginRight',.1,'MarginTop',.1,'MarginBottom',.1, ...
45 Args=parseArgs(varargin,Args,{
'Holdaxis'},{
'Spacing' {
'sh',
'sv'};
'Padding' {
'pl',
'pr',
'pt',
'pb'};
'Margin' {
'ml',
'mr',
'mt',
'mb'}});
47 if (length(Args.NumericArguments)>1)
48 Args.rows=Args.NumericArguments{1};
49 Args.cols=Args.NumericArguments{2};
50 %
remove these 2 numerical arguments
51 Args.NumericArguments={Args.NumericArguments{3:end}};
55 set(f,
'UserData',Args);
61 switch length(Args.NumericArguments)
63 return % no arguments but rows/cols....
65 x1=mod((Args.NumericArguments{1}-1),Args.cols)+1; x2=x1;
66 y1=floor((Args.NumericArguments{1}-1)/Args.cols)+1; y2=y1;
68 x1=Args.NumericArguments{1};x2=x1;
69 y1=Args.NumericArguments{2};y2=y1;
71 x1=Args.NumericArguments{1};x2=x1+Args.NumericArguments{3}-1;
72 y1=Args.NumericArguments{2};y2=y1+Args.NumericArguments{4}-1;
74 error(
'subaxis argument error')
78 cellwidth=((1-Args.MarginLeft-Args.MarginRight)-(Args.cols-1)*Args.SpacingHorizontal)/Args.cols;
79 cellheight=((1-Args.MarginTop-Args.MarginBottom)-(Args.rows-1)*Args.SpacingVertical)/Args.rows;
80 xpos1=Args.MarginLeft+Args.PaddingLeft+cellwidth*(x1-1)+Args.SpacingHorizontal*(x1-1);
81 xpos2=Args.MarginLeft-Args.PaddingRight+cellwidth*x2+Args.SpacingHorizontal*(x2-1);
82 ypos1=Args.MarginTop+Args.PaddingTop+cellheight*(y1-1)+Args.SpacingVertical*(y1-1);
83 ypos2=Args.MarginTop-Args.PaddingBottom+cellheight*y2+Args.SpacingVertical*(y2-1);
86 h=axes('position',[xpos1 1-ypos2 xpos2-xpos1 ypos2-ypos1]);
88 h=subplot('position',[xpos1 1-ypos2 xpos2-xpos1 ypos2-ypos1]);
93 %h=axes('position',[x1 1-y2 x2-x1 y2-y1]);
94 set(h,'units',get(gcf,'defaultaxesunits'));
95 set(h,'tag','subaxis');
99 if (nargout==0) clear h; end;