-
Notifications
You must be signed in to change notification settings - Fork 6
/
getTightSubplotHandles.m
executable file
·79 lines (73 loc) · 2.58 KB
/
getTightSubplotHandles.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
function[hs] = getTightSubplotHandles(FIG_MARGIN,ROW_MARGIN,COLUMN_MARGIN,N_ROWS,N_COLUMNS)
%GETTIGHTSUBPLOTHANDLES return handles to subplots of with the specified spacing
%
% syntax: hs = getTightSubplotHandles(outer_margin,row_margin,...
% column_margin,n_rows,n_columns);
%
% INPUTS:
%
% outer_margin: specifies proportion (between 0 and 1) of the figure to
% use as a border. outer_margin = 0 uses the full figure
% area (no border).
%
% row_margin: specifies proportion (between 0 and 1) of the figure to
% use as a border between subplot rows. row_margin = 0
% sets rows to be directly adjacent (no border).
%
% column_margin: specifies proportion (between 0 and 1) of the figure to
% use as a border between subplot columns. column_margin =
% 0 sets columns to be directly adjacent (no border).
%
% n_rows: number of subplot rows
%
% n_columns: number of subplot columns
%
% OUTPUTS:
%
% hs: handles to the subplots (a 1 by n_rows*n_columns vector)
%
% EXAMPLE:
%
% %create subplots with 1% margins between rows, 2% margins between
% %columns, and a 5% border. create 15 subplots, arranged in 5 rows and 3
% %columns.
% hs = getTightSubplotHandles(0.05,0.01,0.02,5,3);
%
% %plot something in each subplot
% for i = 1:length(hs)
% axes(hs(i));
% plot(rand(1,10),'k','LineWidth',2);
% axis off;
% end
%
% SEE ALSO: PLOT, SUBPLOT, AXES, AXIS
%
% AUTHOR: JEREMY R. MANNING
% CONTACT: [email protected]
%CHANGELOG
% 3-3-12 JRM Wrote it.
assert((FIG_MARGIN >= 0) & (FIG_MARGIN < 1),'specify outer margin between 0 and 1');
assert((ROW_MARGIN >= 0) & (ROW_MARGIN < 1),'specify row margin between 0 and 1');
assert((COLUMN_MARGIN >= 0) & (COLUMN_MARGIN < 1),'specify row margin between 0 and 1');
assert(N_ROWS > 0,'number of rows must be positive');
assert(N_COLUMNS > 0,'number of columns must be positive');
w = 1; %full figure width
h = 1; %full figure height
w = w-(2*FIG_MARGIN)-((N_COLUMNS-1)*COLUMN_MARGIN); %workable width
h = h-(2*FIG_MARGIN)-((N_ROWS-1)*ROW_MARGIN); %workable height
sw = w/N_COLUMNS; %subplot width
sh = h/N_ROWS; %subplot height
hs = zeros(1,N_COLUMNS*N_ROWS);
clf;
count = 1;
startH = 1 - FIG_MARGIN - sh;
for i = 1:N_ROWS
startW = FIG_MARGIN;
for j = 1:N_COLUMNS
h = subplot('position',[startW startH sw sh]);
hs(count) = h;
startW = startW + sw + COLUMN_MARGIN;
count = count+1;
end
startH = startH - sh - ROW_MARGIN;
end