-
Notifications
You must be signed in to change notification settings - Fork 9
/
subplots.m
70 lines (63 loc) · 1.63 KB
/
subplots.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
function subplots(data, layout, names, formatFn)
% Plot each element of a cell array or struct in its own subplot.
%
% subplots(data, layout, names, formatFn)
%
% formatFn will be called for each subplot and it can do whatever it wants
% (e.g. set axes, label axes) with three arguments like subplot:
% formatFn(row, col, i)
if ~exist('layout', 'var'), layout = []; end
if ~exist('names', 'var'), names = {}; end
if ~exist('formatFn', 'var') || isempty(formatFn), formatFn = @(r,c,i) []; end
if isNoDisplay()
% Don't bother...
return
end
if isstruct(data)
names = fieldnames(data);
data = struct2cell(data);
elseif ~iscell(data)
data = {data};
end
nPlots = length(data);
if length(layout) < 2 || all(layout < 0)
nRows = round(sqrt(nPlots));
nCols = ceil(nPlots / nRows);
elseif layout(1) < 0
nCols = layout(2);
nRows = ceil(nPlots / nCols);
elseif layout(2) < 0
nRows = layout(1);
nCols = ceil(nPlots / nRows);
else
nRows = layout(1);
nCols = layout(2);
end
if isempty(names)
names = cellfun(@(x) num2str(x), num2cell(1:nPlots), 'UniformOutput', false);
end
clf
for i = 1:nPlots
subplot(nRows, nCols, i);
x = data{i};
if size(x, 2) < 5
plot(x)
axis tight
elseif size(x, 1) < 5
plot(x')
axis tight
else
imagesc(x)
axis xy
end
colorbar
title(names{i}, 'Interpreter', 'none');
row = floor((i-1) / nCols) + 1;
col = mod(i-1, nCols) + 1;
formatFn(row, col, i);
end
try
%subplot 111
catch
warning('Could not restore subplot to 111')
end