-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsubfigure.m
134 lines (122 loc) · 3.64 KB
/
subfigure.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
function varargout = subfigure(varargin)
% Create a figure within a grid-based layout. It's subplot for figures.
%
% subfigure(m,n,p), or subfigure(mnp), divides the screen into an m-by-n grid of
% tiles and creates a figure within the pth tile. Tiles are counted along the
% top row of the screen, then the second row, etc.
%
% If p is a vector, the figure is sized to cover all the subfigure positions
% listed in p.
%
% subfigure(m,n) creates a figure showing a m-by-n grid layout with tiles
% labeled in the order that they are numbered by subfigure. This is useful for
% planning screen layouts, especially when one or more subfigures will span
% multiple tiles (when p is a vector).
%
% h = subfigure(...) returns a handle to the figure.
%
% Every call to subfigure creates a new figure even if a figure exists at the
% location specified by m, n, and p. The existing figure is not made current or
% reused. Existing figures that are overlapped by new subfigures are not
% deleted. This behavior is dissimilar to subplot.
%
% Example 1: Four non-overlapping figures.
%
% subfigure(2,2,1)
% subfigure(2,2,2)
% subfigure(2,2,3)
% subfigure(2,2,4)
%
% Example 2: Three non-overlapping figures of various sizes.
%
% subfigure(4,4,[1 13])
% subfigure(4,4,[2 4])
% subfigure(4,4,[6 16])
%
% Example 3: Show the grid for a 3 x 5 layout.
%
% subfigure(3,5)
% by Steve Hoelzer
% 2009-03-25
% Padding to allow room for figure borders and menus
hpad = 20;
vpad = 90;
% Process input arguments
switch nargin
case 0
error('Not enough input arguments.')
case 1
m = floor(varargin{1}/100);
n = rem(floor(varargin{1}/10),10);
p = rem(varargin{1},10);
case 2
m = varargin{1};
n = varargin{2};
p = [];
case 3
m = varargin{1};
n = varargin{2};
p = varargin{3};
otherwise
error('Too many input arguments.')
end
% Error checking
if ~isscalar(m) || ~isscalar(n)
error('Gird dimensions must be scalar values.')
end
if m < 0 || n < 0
error('Grid dimensions must be greator than zero.')
end
if any(p > m*n)
error('Position value exceeds grid size.')
end
if isempty(p)
% Draw example grid using subplots
p = m*n;
f = figure('NumberTitle','Off',...
'Name',sprintf('Subfigure tile numbering for a %i by %i grid',m,n));
for i = 1:p
h = subplot(m,n,i);
set(h,'Box','On',...
'XTick',[],...
'YTick',[],...
'XTickLabel',[],...
'YTickLabel',[])
text(0.5,0.5,int2str(i),...
'FontSize',16,...
'HorizontalAlignment','Center')
end
% Return handle if needed
if nargout
varargout{1} = f;
end
return
end
% Calculate tile size and spacing
scrsz = get(0,'ScreenSize');
hstep = floor( (scrsz(3)-hpad) / n );
vstep = floor( (scrsz(4)-vpad) / m );
vsz = vstep - vpad;
hsz = hstep - hpad;
% Row and column positions of each subfigure in p
r = ceil(p/n);
c = rem(p,n);
c(c==0) = n; % Special case
% Position of each subfigure in p in pixels (left, right, bottom, top)
le = hpad + (c-1)*hstep;
ri = le + hsz;
bo = vpad + (m-r)*vstep;
to = bo + vsz;
% Position of a subfigure that covers all subfigures in p
le = min(le); % Leftmost left
ri = max(ri); % Rightmost right
bo = min(bo); % Lowest bottom
to = max(to); % Highest top
% Calculate figure position
pos = [le, bo, ri-le, to-bo]; % [left, bottom, width, height]
% Display figure
h = figure('Position',pos);
% Return handle if needed
if nargout
varargout{1} = h;
end