|
20 | 20 | function handle = afigure(h, config)
|
21 | 21 | if nargin == 0
|
22 | 22 | h = figure;
|
23 |
| - config = aconfig; |
| 23 | + config = []; |
24 | 24 | elseif nargin == 1
|
25 |
| - if ishandle(h) |
26 |
| - h = figure(h); |
27 |
| - config = aconfig; |
28 |
| - else |
| 25 | + if isstruct(h) |
29 | 26 | config = h;
|
30 | 27 | h = figure;
|
| 28 | + else |
| 29 | + figure(h); |
| 30 | + config = []; |
31 | 31 | end
|
32 | 32 | else
|
33 |
| - if ishandle(h) |
34 |
| - figure(h); |
35 |
| - else |
| 33 | + if isstruct(h) |
36 | 34 | h2 = config;
|
37 | 35 | config = h;
|
38 | 36 | h = figure(h2);
|
| 37 | + else |
| 38 | + figure(h); |
39 | 39 | end
|
40 | 40 | end
|
41 | 41 |
|
42 | 42 | if nargout > 1
|
43 | 43 | handle = h;
|
44 | 44 | end
|
45 | 45 |
|
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 |
47 | 56 |
|
48 | 57 |
|
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) |
79 | 59 |
|
80 | 60 | set(h, 'Color', config.BackgroundColor);
|
81 |
| - set(h, 'Colormap', colors); |
| 61 | + set(h, 'Colormap', getColors(config.Colormap)); |
82 | 62 | set(h, 'DefaultAxesColorOrder', colors);
|
83 | 63 | set(h, 'DefaultAxesLineStyleOrder', config.LineStyles);
|
84 | 64 | set(h, 'DefaultAxesFontSize', config.FontSize);
|
@@ -115,3 +95,64 @@ function apply_config(h, config)
|
115 | 95 | elseif ~isempty(config.SizeRatio)
|
116 | 96 | set(h, 'Position', [p(1:3), p(3) / config.SizeRatio]);
|
117 | 97 | 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 |
0 commit comments