|
1 | 1 | function [axis, exponentFormat] = extractAxisData(obj,axisData,axisName)
|
2 | 2 | % extract information related to each axis
|
3 | 3 | % axisData is the data extrated from the figure, axisName take the
|
4 |
| - % values 'x' 'y' or 'z' |
| 4 | + % values "x" "y" or "z" |
5 | 5 |
|
6 |
| - %=========================================================================% |
| 6 | + %=====================================================================% |
7 | 7 | %
|
8 | 8 | % AXIS INITIALIZATION
|
9 | 9 | %
|
10 |
| - %=========================================================================% |
| 10 | + %=====================================================================% |
11 | 11 |
|
12 | 12 | %-general axis settings-%
|
13 |
| - axisColor = 255 * eval(sprintf('axisData.%sColor', axisName)); |
14 |
| - axisColor = sprintf('rgb(%f,%f,%f)', axisColor); |
15 |
| - lineWidth = max(1,axisData.LineWidth*obj.PlotlyDefaults.AxisLineIncreaseFactor); |
| 13 | + axisColor = 255 * axisData.(axisName + "Color"); |
| 14 | + axisColor = sprintf("rgb(%f,%f,%f)", axisColor); |
| 15 | + lineWidth = max(1, ... |
| 16 | + axisData.LineWidth*obj.PlotlyDefaults.AxisLineIncreaseFactor); |
16 | 17 |
|
17 | 18 | try
|
18 |
| - exponentFormat = eval(sprintf('axisData.%sAxis.Exponent', axisName)); |
| 19 | + exponentFormat = axisData.(axisName + "Axis").Exponent; |
19 | 20 | catch
|
20 | 21 | exponentFormat = 0;
|
21 | 22 | end
|
22 | 23 |
|
23 |
| - axis.side = eval(sprintf('axisData.%sAxisLocation', axisName)); |
| 24 | + axis.side = axisData.(axisName + "AxisLocation"); |
24 | 25 | axis.zeroline = false;
|
25 | 26 | axis.autorange = false;
|
26 | 27 | axis.linecolor = axisColor;
|
27 | 28 | axis.linewidth = lineWidth;
|
28 | 29 | axis.exponentformat = obj.PlotlyDefaults.ExponentFormat;
|
29 | 30 |
|
30 |
| - %-------------------------------------------------------------------------% |
| 31 | + %---------------------------------------------------------------------% |
31 | 32 |
|
32 | 33 | %-general tick settings-%
|
33 |
| - tickRotation = eval(sprintf('axisData.%sTickLabelRotation', axisName)); |
| 34 | + tickRotation = axisData.(axisName + "TickLabelRotation"); |
34 | 35 | tickLength = min(obj.PlotlyDefaults.MaxTickLength,...
|
35 | 36 | max(axisData.TickLength(1)*axisData.Position(3)*obj.layout.width,...
|
36 | 37 | axisData.TickLength(1)*axisData.Position(4)*obj.layout.height));
|
|
45 | 46 | axis.tickangle = -tickRotation;
|
46 | 47 |
|
47 | 48 | switch axisData.TickDir
|
48 |
| - case 'in' |
49 |
| - axis.ticks = 'inside'; |
50 |
| - case 'out' |
51 |
| - axis.ticks = 'outside'; |
| 49 | + case "in" |
| 50 | + axis.ticks = "inside"; |
| 51 | + case "out" |
| 52 | + axis.ticks = "outside"; |
52 | 53 | end
|
53 | 54 |
|
54 |
| - %-------------------------------------------------------------------------% |
| 55 | + %---------------------------------------------------------------------% |
55 | 56 |
|
56 | 57 | %-set axis grid-%
|
57 |
| - isGrid = eval(sprintf('axisData.%sGrid', axisName)); |
58 |
| - isMinorGrid = eval(sprintf('axisData.%sMinorGrid', axisName)); |
| 58 | + isGrid = axisData.(axisName + "Grid"); |
| 59 | + isMinorGrid = axisData.(axisName + "MinorGrid"); |
59 | 60 |
|
60 |
| - if strcmp(isGrid, 'on') || strcmp(isMinorGrid, 'on') |
| 61 | + if strcmp(isGrid, "on") || strcmp(isMinorGrid, "on") |
61 | 62 | axis.showgrid = true;
|
62 | 63 | axis.gridwidth = lineWidth;
|
63 | 64 | else
|
64 | 65 | axis.showgrid = false;
|
65 | 66 | end
|
66 | 67 |
|
67 |
| - %-------------------------------------------------------------------------% |
| 68 | + %---------------------------------------------------------------------% |
68 | 69 |
|
69 | 70 | %-axis grid color-%
|
70 | 71 | try
|
71 | 72 | gridColor = 255*axisData.GridColor;
|
72 | 73 | gridAlpha = axisData.GridAlpha;
|
73 |
| - axis.gridcolor = sprintf('rgba(%f,,%f,%f,%f)', gridColor, gridAlpha); |
| 74 | + axis.gridcolor = sprintf("rgba(%f,%f,%f,%f)", gridColor, gridAlpha); |
74 | 75 | catch
|
75 | 76 | axis.gridcolor = axisColor;
|
76 | 77 | end
|
77 | 78 |
|
78 |
| - %-------------------------------------------------------------------------% |
| 79 | + %---------------------------------------------------------------------% |
79 | 80 |
|
80 | 81 | %-axis type-%
|
81 |
| - axis.type = eval(sprintf('axisData.%sScale', axisName)); |
| 82 | + axis.type = axisData.(axisName + "Scale"); |
82 | 83 |
|
83 |
| - %=========================================================================% |
| 84 | + %=====================================================================% |
84 | 85 | %
|
85 | 86 | % SET TICK LABELS
|
86 | 87 | %
|
87 |
| - %=========================================================================% |
| 88 | + %=====================================================================% |
88 | 89 |
|
89 | 90 | %-get tick label data-%
|
90 |
| - tickLabels = eval(sprintf('axisData.%sTickLabel', axisName)); |
91 |
| - tickValues = eval(sprintf('axisData.%sTick', axisName)); |
| 91 | + tickLabels = axisData.(axisName + "TickLabel"); |
| 92 | + tickValues = axisData.(axisName + "Tick"); |
92 | 93 |
|
93 |
| - %-------------------------------------------------------------------------% |
| 94 | + %---------------------------------------------------------------------% |
94 | 95 |
|
95 | 96 | %-there is not tick label case-%
|
96 | 97 | if isempty(tickValues)
|
97 |
| - |
98 |
| - axis.ticks = ''; |
| 98 | + axis.ticks = ""; |
99 | 99 | axis.showticklabels = false;
|
100 | 100 | axis.autorange = true;
|
101 |
| - |
| 101 | + |
102 | 102 | switch axisData.Box
|
103 |
| - case 'on' |
| 103 | + case "on" |
104 | 104 | axis.mirror = true;
|
105 |
| - case 'off' |
| 105 | + case "off" |
106 | 106 | axis.mirror = false;
|
107 | 107 | end
|
108 |
| - |
109 |
| - %-------------------------------------------------------------------------% |
110 |
| - |
111 |
| - %-there is tick labels case-% |
112 |
| - else |
113 |
| - |
| 108 | + else %-there is tick labels case-% |
114 | 109 | %-set tick values-%
|
115 | 110 | axis.showticklabels = true;
|
116 |
| - axis.tickmode = 'array'; |
| 111 | + axis.tickmode = "array"; |
117 | 112 |
|
118 | 113 | if ~iscategorical(tickValues)
|
119 | 114 | axis.tickvals = tickValues;
|
120 | 115 | end
|
121 | 116 |
|
122 | 117 | %-set axis limits-%
|
123 |
| - axisLim = eval( sprintf('axisData.%sLim', axisName) ); |
| 118 | + axisLim = axisData.(axisName + "Lim"); |
124 | 119 |
|
125 | 120 | if isnumeric(axisLim)
|
126 |
| - if strcmp(axis.type, 'linear') |
| 121 | + if any(~isfinite(axisLim)) |
| 122 | + axis.range = shrinkInfLimits(axisData, axisLim, axisName); |
| 123 | + elseif strcmp(axis.type, "linear") |
127 | 124 | axis.range = axisLim;
|
128 |
| - elseif strcmp(axis.type, 'log') |
| 125 | + elseif strcmp(axis.type, "log") |
129 | 126 | axis.range = log10(axisLim);
|
130 | 127 | end
|
131 |
| - |
132 | 128 | elseif isduration(axisLim)
|
133 | 129 | [temp,type] = convertDuration(axisLim);
|
134 | 130 | if (~isduration(temp)) % duration class has specified .Format
|
135 | 131 | axis.range = temp;
|
136 |
| - axis.type = 'duration'; |
| 132 | + axis.type = "duration"; |
137 | 133 | axis.title = type;
|
138 | 134 | axis.tickvals = convertDuration(axis.tickvals);
|
139 | 135 | else
|
140 |
| - nticks = eval(['length(axisData.' axisName 'Tick)-1;']); |
| 136 | + nticks = length(axisData.(axisName + "Tick"))-1; |
141 | 137 | delta = 0.1;
|
142 | 138 | axis.range = [-delta nticks+delta];
|
143 |
| - axis.type = 'duration - specified format'; |
| 139 | + axis.type = "duration - specified format"; |
144 | 140 | end
|
145 |
| - |
146 | 141 | elseif isdatetime(axisLim)
|
147 | 142 | axis.range = axisLim;
|
148 |
| - axis.type = 'date'; |
149 |
| - |
| 143 | + axis.type = "date"; |
150 | 144 | elseif iscategorical(axisLim)
|
151 | 145 | axis.autorange = true;
|
152 |
| - axis.type = 'category'; |
153 |
| - |
| 146 | + axis.type = "category"; |
154 | 147 | else
|
155 | 148 | axis.autorange = true;
|
156 | 149 | end
|
157 | 150 |
|
158 | 151 | %-box setting-%
|
159 | 152 | switch axisData.Box
|
160 |
| - case 'on' |
161 |
| - axis.mirror = 'ticks'; |
162 |
| - case 'off' |
| 153 | + case "on" |
| 154 | + axis.mirror = "ticks"; |
| 155 | + case "off" |
163 | 156 | axis.mirror = false;
|
164 | 157 | end
|
165 | 158 |
|
|
171 | 164 |
|
172 | 165 | %---------------------------------------------------------------------%
|
173 | 166 |
|
174 |
| - %+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++% |
175 |
| - % |
176 |
| - % TODO: determine if following code piece is necessary. For this we need |
177 |
| - % to test fig2plotly with more examples |
178 |
| - % |
179 |
| - %+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++% |
180 |
| - |
181 |
| - % %-LOG TYPE-% |
182 |
| - % if strcmp(axis.type,'log') |
183 |
| - |
184 |
| - % axis.range = log10(axisLim); |
185 |
| - % axis.autotick = true; |
186 |
| - % axis.nticks = eval(['length(axisData.' axisName 'Tick) + 1;']); |
187 |
| - |
188 |
| - % %---------------------------------------------------------------------% |
189 |
| - |
190 |
| - % %-LINEAR TYPE-% |
191 |
| - % elseif strcmp(axis.type,'linear') |
192 |
| - |
193 |
| - % %-----------------------------------------------------------------% |
194 |
| - |
195 |
| - % % %-get tick label mode-% |
196 |
| - % % tickLabelMode = eval(['axisData.' axisName 'TickLabelMode;']); |
197 |
| - |
198 |
| - % % %-----------------------------------------------------------------% |
199 |
| - |
200 |
| - % % %-AUTO MODE-% |
201 |
| - % % if strcmp(tickLabelMode,'auto') |
202 |
| - |
203 |
| - % %-------------------------------------------------------------% |
204 |
| - |
205 |
| - % if isnumeric(axisLim) |
206 |
| - % %-axis range-% |
207 |
| - % axis.range = axisLim; |
208 |
| - % %-axis tickvals-% |
209 |
| - % axis.tickvals = tick; |
210 |
| - |
211 |
| - % %-------------------------------------------------------------% |
212 |
| - |
213 |
| - % elseif isduration(axisLim) |
214 |
| - % [temp,type] = convertDuration(axisLim); |
215 |
| - |
216 |
| - % if (~isduration(temp)) |
217 |
| - % axis.range = temp; |
218 |
| - % axis.type = 'duration'; |
219 |
| - % axis.title = type; |
220 |
| - % else |
221 |
| - % nticks = eval(['length(axisData.' axisName 'Tick)-1;']); |
222 |
| - % delta = 0.1; |
223 |
| - % axis.range = [-delta nticks+delta]; |
224 |
| - % axis.type = 'duration - specified format'; |
225 |
| - % end |
226 |
| - |
227 |
| - % %-------------------------------------------------------------% |
228 |
| - |
229 |
| - % elseif isdatetime(axisLim) |
230 |
| - % axis.range = convertDate(axisLim); |
231 |
| - % axis.type = 'date'; |
232 |
| - % else |
233 |
| - % % data is a category type other then duration and datetime |
234 |
| - % end |
235 |
| - |
236 |
| - % %-------------------------------------------------------------% |
237 |
| - |
238 |
| - % if ~isnumeric(axisLim) |
239 |
| - % %-axis autotick-% |
240 |
| - % axis.autotick = true; |
241 |
| - % %-axis numticks-% |
242 |
| - % axis.nticks = eval(['length(axisData.' axisName 'Tick)+1']); |
243 |
| - % end |
244 |
| - % end |
245 |
| - % end |
246 |
| - |
247 |
| - %+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++% |
248 |
| - |
249 |
| - %-------------------------------------------------------------------------% |
250 |
| - |
251 | 167 | %-axis direction-%
|
252 |
| - axisDirection = eval(sprintf('axisData.%sDir', axisName)); |
| 168 | + axisDirection = axisData.(axisName + "Dir"); |
253 | 169 |
|
254 |
| - if strcmp(axisDirection,'reverse') |
| 170 | + if strcmp(axisDirection, "reverse") |
255 | 171 | axis.range = [axis.range(2) axis.range(1)];
|
256 | 172 | end
|
257 | 173 |
|
258 |
| - %=========================================================================% |
| 174 | + %=====================================================================% |
259 | 175 | %
|
260 | 176 | % SET AXIS LABEL
|
261 | 177 | %
|
262 |
| - %=========================================================================% |
| 178 | + %=====================================================================% |
263 | 179 |
|
264 | 180 | %-get label data-%
|
265 |
| - label = eval(sprintf('axisData.%sLabel', axisName)); |
| 181 | + label = axisData.(axisName + "Label"); |
266 | 182 | labelData = label;
|
267 | 183 |
|
268 |
| - %-------------------------------------------------------------------------% |
| 184 | + %---------------------------------------------------------------------% |
269 | 185 |
|
270 | 186 | %-STANDARDIZE UNITS-%
|
271 | 187 | fontunits = label.FontUnits;
|
272 |
| - set(label,'FontUnits','points'); |
| 188 | + label.FontUnits = "points"; |
273 | 189 |
|
274 |
| - %-------------------------------------------------------------------------% |
| 190 | + %---------------------------------------------------------------------% |
275 | 191 |
|
276 | 192 | %-title label settings-%
|
277 | 193 | if ~isempty(labelData.String)
|
278 | 194 | axis.title = parseString(labelData.String,labelData.Interpreter);
|
279 | 195 | end
|
280 | 196 |
|
281 |
| - axis.titlefont.color = sprintf('rgb(%f,%f,%f)', 255*labelData.Color); |
| 197 | + axis.titlefont.color = sprintf("rgb(%f,%f,%f)", 255*labelData.Color); |
282 | 198 | axis.titlefont.size = labelData.FontSize;
|
283 | 199 | axis.titlefont.family = matlab2plotlyfont(labelData.FontName);
|
284 | 200 |
|
285 |
| - %-------------------------------------------------------------------------% |
| 201 | + %---------------------------------------------------------------------% |
286 | 202 |
|
287 | 203 | %-REVERT UNITS-%
|
288 |
| - set(label,'FontUnits',fontunits); |
| 204 | + label.FontUnits = fontunits; |
289 | 205 |
|
290 |
| - %-------------------------------------------------------------------------% |
| 206 | + %---------------------------------------------------------------------% |
291 | 207 |
|
292 | 208 | %-set visibility conditions-%
|
293 |
| - if strcmp(axisData.Visible,'on') |
| 209 | + if strcmp(axisData.Visible, "on") |
294 | 210 | axis.showline = true;
|
295 | 211 | else
|
296 | 212 | axis.showticklabels = false;
|
297 | 213 | axis.showline = false;
|
298 |
| - axis.ticks = ''; |
| 214 | + axis.ticks = ""; |
299 | 215 | end
|
| 216 | +end |
300 | 217 |
|
301 |
| - %-------------------------------------------------------------------------% |
| 218 | +function lim = shrinkInfLimits(axis, lim, axisName) |
| 219 | + arguments |
| 220 | + axis |
| 221 | + lim |
| 222 | + axisName (1,1) string {mustBeMember(axisName,["Y" "X"])} |
| 223 | + end |
| 224 | + plots = axis.Children; |
| 225 | + plots = plots(~arrayfun( ... |
| 226 | + @(x) isa(x,"matlab.graphics.chart.decoration.ConstantLine"), ... |
| 227 | + plots)); |
| 228 | + if ~isempty(plots) |
| 229 | + dataRange = [Inf -Inf]; |
| 230 | + for i = 1:numel(plots) |
| 231 | + dataRange(1) = min(dataRange(1),min(plots(i).(axisName+"Data"))); |
| 232 | + dataRange(2) = max(dataRange(2),max(plots(i).(axisName+"Data"))); |
| 233 | + end |
| 234 | + dataRange = dataRange + [-1 1]*diff(dataRange)/8; % add some margin |
| 235 | + else |
| 236 | + dataRange = [0 1]; % matches default y-axis from `figure; xline(1)` |
| 237 | + end |
| 238 | + toShrink = ~isfinite(lim); |
| 239 | + lim(toShrink) = dataRange(toShrink); |
302 | 240 | end
|
0 commit comments