-
Notifications
You must be signed in to change notification settings - Fork 14
/
afigure.m
158 lines (144 loc) · 4.45 KB
/
afigure.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
% AFIGURE Create an Academic Figure.
% AFIGURE creates a figure with the enhanced color and text
% configuration by default, as returned by function aconfig.
%
% AFIGURE(H) makes H the current figure, as figure(h).
%
% AFIGURE(config) creates a figure with the color and text configuration
% given by the structure config. This can be obtained from function
% aconfig.
%
% AFIGURE(H, config) makes H the current figure and sets the given
% configuration.
%
% AFIGURE(config, H) is an alias of AFIGURE(H, config).
%
% handle = AFIGURE(...) returns the handle of the figure.
%
% See also aconfig, aplot, abar
%
function handle = afigure(h, config)
if nargin == 0
h = figure;
config = [];
elseif nargin == 1
if isstruct(h)
config = h;
h = figure;
else
figure(h);
config = [];
end
else
if isstruct(h)
h2 = config;
config = h;
h = figure(h2);
else
figure(h);
end
end
if nargout > 1
handle = h;
end
if ~isempty(config)
colors = getColors(config.Colormap);
guidata(h, config);
apply_config_fig(h, config, colors);
elseif isempty(guidata(h))
config = aconfig;
colors = getColors(config.Colormap);
guidata(h, config);
apply_config_fig(h, config, colors);
end
function apply_config_fig(h, config, colors)
set(h, 'Color', config.BackgroundColor);
set(h, 'Colormap', getColors(config.Colormap));
set(h, 'DefaultAxesColorOrder', colors);
set(h, 'DefaultAxesLineStyleOrder', config.LineStyles);
set(h, 'DefaultAxesFontSize', config.FontSize);
set(h, 'DefaultLineLineWidth', config.LineWidth);
if config.RemoveMargins
set(h, 'DefaultAxesLooseInset', [0 0 0 0]);
end
if config.DrawBox
set(h, 'DefaultAxesBox', 'on');
else
set(h, 'DefaultAxesBox', 'off');
end
if config.Grid
set(h, 'DefaultAxesXGrid', 'on', 'DefaultAxesYGrid', 'on', ...
'DefaultAxesZGrid', 'on');
else
set(h, 'DefaultAxesXGrid', 'off', 'DefaultAxesYGrid', 'off', ...
'DefaultAxesZGrid', 'off');
end
p = get(h, 'Position');
if ~isempty(config.Width) && ~isempty(config.Height)
set(h, 'Position', [p(1:2), config.Width, config.Height]);
elseif ~isempty(config.Width) && ~isempty(config.SizeRatio)
set(h, 'Position', [p(1:2), config.Width, ...
config.Width / config.SizeRatio]);
elseif ~isempty(config.Height) && ~isempty(config.SizeRatio)
set(h, 'Position', [p(1:2), config.Height * config.SizeRatio, ...
config.Height]);
elseif ~isempty(config.Width)
set(h, 'Position', [p(1:2), config.Width, p(4)]);
elseif ~isempty(config.Height)
set(h, 'Position', [p(1:3), config.Height]);
elseif ~isempty(config.SizeRatio)
set(h, 'Position', [p(1:3), p(3) / config.SizeRatio]);
end
ha = get(h, 'CurrentAxes');
if ~isempty(ha)
apply_config_axis(ha, config, colors);
end
function apply_config_axis(h, config, colors)
hh = [h; get(h, 'XLabel'); get(h, 'YLabel'); get(h, 'ZLabel'); ...
get(h, 'Title')];
set(hh, 'FontSize', config.FontSize);
set(h, 'ColorOrder', getColors(config.Colormap));
set(h, 'LineStyleOrder', config.LineStyles);
set(h, 'FontSize', config.FontSize);
if config.DrawBox
set(h, 'Box', 'on');
else
set(h, 'Box', 'off');
end
if config.Grid
set(h, 'XGrid', 'on', 'YGrid', 'on', 'ZGrid', 'on');
else
set(h, 'XGrid', 'off', 'YGrid', 'off', 'ZGrid', 'off');
end
if config.RemoveMargins
set(h, 'LooseInset', [0 0 0 0]);
end
function colors = getColors(colormap)
% Original authors of colormaps:
% http://www.mathworks.com/matlabcentral/fileexchange/2662
% http://www.mathworks.com/matlabcentral/fileexchange/41583
colors = [];
if ischar(colormap)
while isempty(colors)
switch colormap
case 'thermal'
colors = [ 0 0 0; 0.3 0 0.7; 1 0.2 0; 0.9 0.9 0 ];
case 'cmr'
colors = [ 0 0 0; 1 0.25 0.15; 0.9 0.5 0; 0.9 0.75 0.1; ...
0.85 0.85 0.5 ];
case 'dusk'
colors = [ 0 0 0; 0 0 0.5; 0 0.5 0.5; 0.5 0.5 0.5; 1 0.5 0.5; ...
1 1 0.5 ];
case 'hsv2'
colors = [ 0 0 0; 0.5 0 0.5; 0 0 0.9; 0 1 1; 0 1 0; 1 1 0; 1 0 0 ];
case 'gray'
colors = [ 0 0 0; 0.4 0.4 0.4; 0.65 0.65 0.65; 0.8 0.8 0.8 ];
otherwise
warning('AcademicFigures:colormap', ...
'Unknown colormap: %s', colormap);
colormap = 'cmr';
end
end
else
colors = colormap;
end