Skip to content

Commit 8b55b8a

Browse files
author
Dorian Galvez-Lopez
committed
Fix: afigure with new figure as a parameter.
aplot and abar set the style of an already existing figure.
1 parent e6fc2d1 commit 8b55b8a

File tree

3 files changed

+83
-51
lines changed

3 files changed

+83
-51
lines changed

abar.m

+1-6
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,8 @@
66
%
77
% See also afigure, aconfig, aplot
88
function handles = abar(varargin)
9-
if isempty(findobj('type', 'figure'))
10-
afigure;
11-
end
12-
9+
afigure(gcf);
1310
hnds = bar(varargin{:});
14-
1511
if nargout > 0
1612
handles = hnds;
1713
end
18-

afigure.m

+81-40
Original file line numberDiff line numberDiff line change
@@ -20,65 +20,45 @@
2020
function handle = afigure(h, config)
2121
if nargin == 0
2222
h = figure;
23-
config = aconfig;
23+
config = [];
2424
elseif nargin == 1
25-
if ishandle(h)
26-
h = figure(h);
27-
config = aconfig;
28-
else
25+
if isstruct(h)
2926
config = h;
3027
h = figure;
28+
else
29+
figure(h);
30+
config = [];
3131
end
3232
else
33-
if ishandle(h)
34-
figure(h);
35-
else
33+
if isstruct(h)
3634
h2 = config;
3735
config = h;
3836
h = figure(h2);
37+
else
38+
figure(h);
3939
end
4040
end
4141

4242
if nargout > 1
4343
handle = h;
4444
end
4545

46-
apply_config(h, config);
46+
if ~isempty(config)
47+
colors = getColors(config.Colormap);
48+
guidata(h, config);
49+
apply_config_fig(h, config, colors);
50+
elseif isempty(guidata(h))
51+
config = aconfig;
52+
colors = getColors(config.Colormap);
53+
guidata(h, config);
54+
apply_config_fig(h, config, colors);
55+
end
4756

4857

49-
function apply_config(h, config)
50-
51-
% Original authors of colormaps:
52-
% http://www.mathworks.com/matlabcentral/fileexchange/2662
53-
% http://www.mathworks.com/matlabcentral/fileexchange/41583
54-
colors = [];
55-
if ischar(config.Colormap)
56-
while isempty(colors)
57-
switch config.Colormap
58-
case 'thermal'
59-
colors = [ 0 0 0; 0.3 0 0.7; 1 0.2 0; 0.9 0.9 0 ];
60-
case 'cmr'
61-
colors = [ 0 0 0; 1 0.25 0.15; 0.9 0.5 0; 0.9 0.75 0.1; ...
62-
0.85 0.85 0.5 ];
63-
case 'dusk'
64-
colors = [ 0 0 0; 0 0 0.5; 0 0.5 0.5; 0.5 0.5 0.5; 1 0.5 0.5; ...
65-
1 1 0.5 ];
66-
case 'hsv2'
67-
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 ];
68-
case 'gray'
69-
colors = [ 0 0 0; 0.4 0.4 0.4; 0.65 0.65 0.65; 0.8 0.8 0.8 ];
70-
otherwise
71-
warning('AcademicFigures:colormap', ...
72-
'Unknown colormap: %s', config.Colormap);
73-
config.Colormap = 'cmr';
74-
end
75-
end
76-
else
77-
colors = config.Colormap;
78-
end
58+
function apply_config_fig(h, config, colors)
7959

8060
set(h, 'Color', config.BackgroundColor);
81-
set(h, 'Colormap', colors);
61+
set(h, 'Colormap', getColors(config.Colormap));
8262
set(h, 'DefaultAxesColorOrder', colors);
8363
set(h, 'DefaultAxesLineStyleOrder', config.LineStyles);
8464
set(h, 'DefaultAxesFontSize', config.FontSize);
@@ -115,3 +95,64 @@ function apply_config(h, config)
11595
elseif ~isempty(config.SizeRatio)
11696
set(h, 'Position', [p(1:3), p(3) / config.SizeRatio]);
11797
end
98+
99+
ha = get(h, 'CurrentAxes');
100+
if ~isempty(ha)
101+
apply_config_axis(ha, config, colors);
102+
end
103+
104+
function apply_config_axis(h, config, colors)
105+
106+
hh = [h; get(h, 'XLabel'); get(h, 'YLabel'); get(h, 'ZLabel'); ...
107+
get(h, 'Title')];
108+
set(hh, 'FontSize', config.FontSize);
109+
110+
set(h, 'ColorOrder', getColors(config.Colormap));
111+
set(h, 'LineStyleOrder', config.LineStyles);
112+
set(h, 'FontSize', config.FontSize);
113+
114+
if config.DrawBox
115+
set(h, 'Box', 'on');
116+
else
117+
set(h, 'Box', 'off');
118+
end
119+
120+
if config.Grid
121+
set(h, 'XGrid', 'on', 'YGrid', 'on', 'ZGrid', 'on');
122+
else
123+
set(h, 'XGrid', 'off', 'YGrid', 'off', 'ZGrid', 'off');
124+
end
125+
126+
if config.RemoveMargins
127+
set(h, 'LooseInset', [0 0 0 0]);
128+
end
129+
130+
function colors = getColors(colormap)
131+
% Original authors of colormaps:
132+
% http://www.mathworks.com/matlabcentral/fileexchange/2662
133+
% http://www.mathworks.com/matlabcentral/fileexchange/41583
134+
colors = [];
135+
if ischar(colormap)
136+
while isempty(colors)
137+
switch colormap
138+
case 'thermal'
139+
colors = [ 0 0 0; 0.3 0 0.7; 1 0.2 0; 0.9 0.9 0 ];
140+
case 'cmr'
141+
colors = [ 0 0 0; 1 0.25 0.15; 0.9 0.5 0; 0.9 0.75 0.1; ...
142+
0.85 0.85 0.5 ];
143+
case 'dusk'
144+
colors = [ 0 0 0; 0 0 0.5; 0 0.5 0.5; 0.5 0.5 0.5; 1 0.5 0.5; ...
145+
1 1 0.5 ];
146+
case 'hsv2'
147+
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 ];
148+
case 'gray'
149+
colors = [ 0 0 0; 0.4 0.4 0.4; 0.65 0.65 0.65; 0.8 0.8 0.8 ];
150+
otherwise
151+
warning('AcademicFigures:colormap', ...
152+
'Unknown colormap: %s', colormap);
153+
colormap = 'cmr';
154+
end
155+
end
156+
else
157+
colors = colormap;
158+
end

aplot.m

+1-5
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,8 @@
66
%
77
% See also afigure, aconfig, abar
88
function handles = aplot(varargin)
9-
if isempty(findobj('type', 'figure'))
10-
afigure;
11-
end
12-
9+
afigure(gcf);
1310
hnds = plot(varargin{:});
14-
1511
if nargout > 0
1612
handles = hnds;
1713
end

0 commit comments

Comments
 (0)