Robochameleon  v1.0
subaxis.m
1 function h=subaxis(varargin)
2 %SUBAXIS Create axes in tiled positions. (just like subplot)
3 % Usage:
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])
7 %
8 % SETTINGS: Spacing,SpacingHoriz,SpacingVert
9 % Padding,PaddingRight,PaddingLeft,PaddingTop,PaddingBottom
10 % Margin,MarginRight,MarginLeft,MarginTop,MarginBottom
11 % Holdaxis
12 %
13 % all units are relative (e.g from 0 to 1)
14 %
15 % Abbreviations of parameters can be used.. (Eg MR instead of MarginRight)
16 % (holdaxis means that it wont delete any axes below.)
17 %
18 %
19 % Example:
20 %
21 % >> subaxis(2,1,1,'SpacingVert',0,'MR',0);
22 % >> imagesc(magic(3))
23 % >> subaxis(2,'p',.02);
24 % >> imagesc(magic(4))
25 %
26 % 2001 / Aslak Grinsted (Feel free to modify this code.)
27 f=gcf;
28 
29 
30 Args=[];
31 UserDataArgsOK=0;
32 Args=get(f,'UserData');
33 if isstruct(Args)
34  UserDataArgsOK=isfield(Args,'SpacingHorizontal')&isfield(Args,'Holdaxis')&isfield(Args,'rows')&isfield(Args,'cols');
35 end
36 OKToStoreArgs=isempty(Args)|UserDataArgsOK;
37 
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, ...
43  'rows',[],'cols',[]);
44 end
45 Args=parseArgs(varargin,Args,{'Holdaxis'},{'Spacing' {'sh','sv'}; 'Padding' {'pl','pr','pt','pb'}; 'Margin' {'ml','mr','mt','mb'}});
46 
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}};
52 end
53 
54 if OKToStoreArgs
55  set(f,'UserData',Args);
56 end
57 
58 
59 
60 
61 switch length(Args.NumericArguments)
62  case 0
63  return % no arguments but rows/cols....
64  case 1
65  x1=mod((Args.NumericArguments{1}-1),Args.cols)+1; x2=x1;
66  y1=floor((Args.NumericArguments{1}-1)/Args.cols)+1; y2=y1;
67  case 2
68  x1=Args.NumericArguments{1};x2=x1;
69  y1=Args.NumericArguments{2};y2=y1;
70  case 4
71  x1=Args.NumericArguments{1};x2=x1+Args.NumericArguments{3}-1;
72  y1=Args.NumericArguments{2};y2=y1+Args.NumericArguments{4}-1;
73  otherwise
74  error('subaxis argument error')
75 end
76 
77 
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);
84 
85 if Args.Holdaxis
86  h=axes('position',[xpos1 1-ypos2 xpos2-xpos1 ypos2-ypos1]);
87 else
88  h=subplot('position',[xpos1 1-ypos2 xpos2-xpos1 ypos2-ypos1]);
89 end
90 
91 
92 set(h,'box','on');
93 %h=axes('position',[x1 1-y2 x2-x1 y2-y1]);
94 set(h,'units',get(gcf,'defaultaxesunits'));
95 set(h,'tag','subaxis');
96 
97 
98 
99 if (nargout==0) clear h; end;
100