1 |
function SaveTemplate(path, filename) |
---|
2 |
% |
---|
3 |
% Abstract: |
---|
4 |
% Save current figure template as an m-file function. |
---|
5 |
% |
---|
6 |
% Usage: |
---|
7 |
% >> SaveTemplate([path], filename) |
---|
8 |
% |
---|
9 |
% where [<path>] is the directory path string of the location to store the |
---|
10 |
% function by the name provided with <filename>. If no [path] is specified |
---|
11 |
% then the current path is taken. '.m' is appended if only the prefix of |
---|
12 |
% the name is given in <filename>. |
---|
13 |
% |
---|
14 |
% A MATLAB script function is created from the skeletal elements |
---|
15 |
% that make up a figure window, which reproduces the following: |
---|
16 |
% |
---|
17 |
% o all the figure properties from the original figure |
---|
18 |
% o all the axes and their properties |
---|
19 |
% o all text children of each of the axes and properties of each |
---|
20 |
% text item. |
---|
21 |
% |
---|
22 |
% The SaveAs function does not save any inforamation about any graphical |
---|
23 |
% user interfaces actions, such as any button down functions or uicontrols |
---|
24 |
% or uimenus and their call backs. It also does not save any user data nor |
---|
25 |
% does it retain xdata, ydata, zdata and hence does not plot lines, |
---|
26 |
% patches, surfaces, and images. |
---|
27 |
|
---|
28 |
% |
---|
29 |
% History: |
---|
30 |
% o 15 August 1995 created function SaveTemplate as part of Prmenu, |
---|
31 |
% by Sara Haines. |
---|
32 |
% |
---|
33 |
|
---|
34 |
global LASTFIGURE |
---|
35 |
if isempty(LASTFIGURE) |
---|
36 |
curfig = gcf; |
---|
37 |
else |
---|
38 |
curfig = LASTFIGURE; |
---|
39 |
end |
---|
40 |
set(curfig,'Pointer','watch'); |
---|
41 |
|
---|
42 |
% |
---|
43 |
if nargin == 1 |
---|
44 |
filename = path; |
---|
45 |
path = '.'; |
---|
46 |
elseif nargin > 2 |
---|
47 |
disp('SaveAs: Too many input parameters'); |
---|
48 |
return; |
---|
49 |
elseif nargin == 0 |
---|
50 |
disp('SaveAs: Too many input parameters'); |
---|
51 |
return; |
---|
52 |
end |
---|
53 |
|
---|
54 |
figprop = [ ... |
---|
55 |
'Color ';... |
---|
56 |
'InvertHardcopy ';... |
---|
57 |
'Name ';... |
---|
58 |
'NextPlot ';... |
---|
59 |
'NumberTitle ';... |
---|
60 |
'PaperUnits ';... |
---|
61 |
'PaperOrientation';... |
---|
62 |
'PaperPosition ';... |
---|
63 |
'PaperType ';... |
---|
64 |
'Position ';... |
---|
65 |
'Units ';... |
---|
66 |
'Visible ';... |
---|
67 |
'Clipping ';... |
---|
68 |
'Tag '... |
---|
69 |
]; |
---|
70 |
|
---|
71 |
axesprop = [... |
---|
72 |
'AspectRatio ';... |
---|
73 |
'Box ';... |
---|
74 |
'CLim ';... |
---|
75 |
'CLimMode ';... |
---|
76 |
'Color ';... |
---|
77 |
'ColorOrder ';... |
---|
78 |
'DrawMode ';... |
---|
79 |
'ExpFontAngle ';... |
---|
80 |
'ExpFontName ';... |
---|
81 |
'ExpFontSize ';... |
---|
82 |
'ExpFontStrikeThrough';... |
---|
83 |
'ExpFontUnderline ';... |
---|
84 |
'ExpFontWeight ';... |
---|
85 |
'FontAngle ';... |
---|
86 |
'FontName ';... |
---|
87 |
'FontSize ';... |
---|
88 |
'FontStrikeThrough ';... |
---|
89 |
'FontUnderline ';... |
---|
90 |
'FontWeight ';... |
---|
91 |
'GridLineStyle ';... |
---|
92 |
'Layer ';... |
---|
93 |
'LineStyleOrder ';... |
---|
94 |
'LineWidth ';... |
---|
95 |
'MinorGridLineStyle ';... |
---|
96 |
'NextPlot ';... |
---|
97 |
'TickLength ';... |
---|
98 |
'TickDir ';... |
---|
99 |
'View ';... |
---|
100 |
'XColor ';... |
---|
101 |
'XDir ';... |
---|
102 |
'XGrid ';... |
---|
103 |
'XLim ';... |
---|
104 |
'XLimMode ';... |
---|
105 |
'XMinorGrid ';... |
---|
106 |
'XMinorTicks ';... |
---|
107 |
'XScale ';... |
---|
108 |
'XTick ';... |
---|
109 |
'XTickLabels ';... |
---|
110 |
'XTickLabelMode ';... |
---|
111 |
'XTickMode ';... |
---|
112 |
'YColor ';... |
---|
113 |
'YDir ';... |
---|
114 |
'YGrid ';... |
---|
115 |
'YLim ';... |
---|
116 |
'YLimMode ';... |
---|
117 |
'YMinorGrid ';... |
---|
118 |
'YMinorTicks ';... |
---|
119 |
'YScale ';... |
---|
120 |
'YTick ';... |
---|
121 |
'YTickLabels ';... |
---|
122 |
'YTickLabelMode ';... |
---|
123 |
'YTickMode ';... |
---|
124 |
'ZColor ';... |
---|
125 |
'ZDir ';... |
---|
126 |
'ZGrid ';... |
---|
127 |
'ZLim ';... |
---|
128 |
'ZLimMode ';... |
---|
129 |
'ZMinorGrid ';... |
---|
130 |
'ZMinorTicks ';... |
---|
131 |
'ZScale ';... |
---|
132 |
'ZTick ';... |
---|
133 |
'ZTickLabels ';... |
---|
134 |
'ZTickLabelMode ';... |
---|
135 |
'ZTickMode ';... |
---|
136 |
'Clipping ';... |
---|
137 |
'Tag ';... |
---|
138 |
'Visible ';... |
---|
139 |
]; |
---|
140 |
|
---|
141 |
|
---|
142 |
textprop = [... |
---|
143 |
'Color '; ... |
---|
144 |
'EraseMode '; ... |
---|
145 |
'FontAngle '; ... |
---|
146 |
'FontName '; ... |
---|
147 |
'FontSize '; ... |
---|
148 |
'FontStrikeThrough '; ... |
---|
149 |
'FontUnderline '; ... |
---|
150 |
'FontWeight '; ... |
---|
151 |
'HorizontalAlignment'; ... |
---|
152 |
'Position '; ... |
---|
153 |
'Rotation '; ... |
---|
154 |
'String '; ... |
---|
155 |
'Units '; ... |
---|
156 |
'VerticalAlignment '; ... |
---|
157 |
'Tag '; ... |
---|
158 |
'Visible '; ... |
---|
159 |
]; |
---|
160 |
|
---|
161 |
labeltype = [... |
---|
162 |
'Title '; ... |
---|
163 |
'Xlabel'; ... |
---|
164 |
'Ylabel'; ... |
---|
165 |
'Zlabel'; ... |
---|
166 |
]; |
---|
167 |
|
---|
168 |
% append or change suffix to dot-m if not found in filename |
---|
169 |
prefixname = strtok(filename, '.'); |
---|
170 |
if isempty(findstr(filename, '.m')) |
---|
171 |
filename = [prefixname '.m']; |
---|
172 |
end |
---|
173 |
|
---|
174 |
% remove any file with same name and path |
---|
175 |
if (exist([path '/' filename]) == 2 ) |
---|
176 |
eval(['delete ''' sprintf('%s',[path '/' filename]) '''']); |
---|
177 |
end |
---|
178 |
|
---|
179 |
[fid, mesg] = fopen([path '/' filename], 'wt'); |
---|
180 |
if (fid) < 0 |
---|
181 |
disp(['SaveAs: Error opening file: ' mesg]); |
---|
182 |
return; |
---|
183 |
end |
---|
184 |
|
---|
185 |
% write function and header comments |
---|
186 |
fprintf(fid, '%s\n', ['function ' prefixname '()']); |
---|
187 |
fprintf(fid, '%s\n', ['% This function was generated by another '... |
---|
188 |
'function. It regenerates']); |
---|
189 |
fprintf(fid, '%s\n', ['% a figure template as saved by function SaveAs.']); |
---|
190 |
fprintf(fid, '%s\n', ['% ']); |
---|
191 |
fprintf(fid, '%s\n', ['% Usage: Type the name of the function. ']); |
---|
192 |
fprintf(fid, '%s\n', ['% >> ' prefixname]); |
---|
193 |
fprintf(fid, '%s\n\n', ['% ']); |
---|
194 |
|
---|
195 |
fprintf(fid, '%s\n\n', ['A = figure;']); |
---|
196 |
|
---|
197 |
[Nfigprop, Mfigprop] = size(figprop); |
---|
198 |
for element=1:Nfigprop |
---|
199 |
prop = deblank(figprop(element,:)); |
---|
200 |
propvalue = get(curfig, prop); |
---|
201 |
if ~isstr(propvalue) |
---|
202 |
[n,m] = size(propvalue); |
---|
203 |
str = '['; |
---|
204 |
for i=1:n |
---|
205 |
if i>1 |
---|
206 |
str = [str, sprintf('\t\t')]; |
---|
207 |
end |
---|
208 |
for j=1:m |
---|
209 |
str = [str, sprintf('%s,', num2str(propvalue(i,j)))]; |
---|
210 |
end |
---|
211 |
if i<n |
---|
212 |
str = [str, sprintf(';...\n')]; |
---|
213 |
else |
---|
214 |
str = [str, ']' ]; |
---|
215 |
end |
---|
216 |
end |
---|
217 |
propstr = [str]; |
---|
218 |
else |
---|
219 |
propstr = ['''' propvalue '''']; |
---|
220 |
end |
---|
221 |
printstr = ['set(A, ''' prop ''', ' propstr ');']; |
---|
222 |
fprintf(fid, '%s\n', printstr); |
---|
223 |
end |
---|
224 |
|
---|
225 |
fprintf(fid, '\n'); % blank line separator |
---|
226 |
figAxes = findobj(curfig, 'type', 'axes'); |
---|
227 |
Naxes = length(figAxes); |
---|
228 |
|
---|
229 |
for eachAxis=1:Naxes |
---|
230 |
% write command to open axes with position and units from original graph |
---|
231 |
% also get and write commands to set labels and title |
---|
232 |
% but since they are text objects set their text properties after all axes |
---|
233 |
% properties are taken care of |
---|
234 |
posstr = sprintf('[%f %f %f %f]', get(figAxes(eachAxis), 'Position')); |
---|
235 |
unitsstr = sprintf('''%s''', get(figAxes(eachAxis), 'Units')); |
---|
236 |
|
---|
237 |
fprintf(fid, '%s\n', 'B = axes(''Parent'', A, ...'); |
---|
238 |
fprintf(fid, '\t%s\n', ['''Position'', ' posstr ', ...']); |
---|
239 |
fprintf(fid, '\t%s\n', ['''Units'', ' unitsstr ');']); |
---|
240 |
|
---|
241 |
% write set commands for each property of axes (except |
---|
242 |
% for those already set above for each axis |
---|
243 |
[Naxesprop, Maxesprop] = size(axesprop); |
---|
244 |
for element=1:Naxesprop |
---|
245 |
prop = deblank(axesprop(element,:)); |
---|
246 |
propvalue = get(figAxes(eachAxis), prop); |
---|
247 |
[n,m] = size(propvalue); |
---|
248 |
if ~isstr(propvalue) |
---|
249 |
str = '['; |
---|
250 |
for i=1:n |
---|
251 |
if i>1 |
---|
252 |
str = [str, sprintf('\t\t')]; |
---|
253 |
end |
---|
254 |
for j=1:m |
---|
255 |
str = [str, sprintf('%s,', num2str(propvalue(i,j)))]; |
---|
256 |
end |
---|
257 |
if i<n |
---|
258 |
str = [str, sprintf('; ...\n')]; |
---|
259 |
else |
---|
260 |
str = [str, ']' ]; |
---|
261 |
end |
---|
262 |
end |
---|
263 |
propstr = [str]; |
---|
264 |
else |
---|
265 |
if n>1 |
---|
266 |
str = '['; |
---|
267 |
for i=1:n |
---|
268 |
if i>1 |
---|
269 |
str = [str, sprintf('\t\t')]; |
---|
270 |
end |
---|
271 |
str = [str, sprintf(['''%' int2str(m) 's'''], propvalue(i,1:m))]; |
---|
272 |
if i<n |
---|
273 |
str = [str, sprintf(';...\n')]; |
---|
274 |
else |
---|
275 |
str = [str, ']' ]; |
---|
276 |
end |
---|
277 |
|
---|
278 |
end %for |
---|
279 |
propstr = [str]; |
---|
280 |
else |
---|
281 |
propstr = ['''' propvalue '''']; |
---|
282 |
end |
---|
283 |
end |
---|
284 |
printstr = ['set(B, ''' prop ''', ' propstr ');']; |
---|
285 |
fprintf(fid, '%s\n', printstr); |
---|
286 |
end |
---|
287 |
|
---|
288 |
fprintf(fid, '\n'); % blank line separatro |
---|
289 |
|
---|
290 |
% find all text objects of the current axes |
---|
291 |
TextObjs = findobj(figAxes(eachAxis), 'type', 'text'); |
---|
292 |
|
---|
293 |
% determine which ones are added text childern or title or x-, |
---|
294 |
% y-, or z-labels |
---|
295 |
titlHndl = get(figAxes(eachAxis), 'title'); |
---|
296 |
xlabHndl = get(figAxes(eachAxis), 'xlabel'); |
---|
297 |
ylabHndl = get(figAxes(eachAxis), 'ylabel'); |
---|
298 |
zlabHndl = get(figAxes(eachAxis), 'zlabel'); |
---|
299 |
LabelObjs = [titlHndl xlabHndl ylabHndl zlabHndl]; |
---|
300 |
TextObjs(find(TextObjs==titlHndl)) = []; |
---|
301 |
TextObjs(find(TextObjs==xlabHndl)) = []; |
---|
302 |
TextObjs(find(TextObjs==ylabHndl)) = []; |
---|
303 |
TextObjs(find(TextObjs==zlabHndl)) = []; |
---|
304 |
|
---|
305 |
% update the properties for title, and labels |
---|
306 |
for eachText=1:4 |
---|
307 |
Hndlstr = deblank(labeltype(eachText,:)); |
---|
308 |
fprintf(fid, '%s\n', [Hndlstr 'Hndl = text;']); |
---|
309 |
[Ntextprop, Mtextprop] = size(textprop); |
---|
310 |
for element=1:Ntextprop |
---|
311 |
prop = deblank(textprop(element,:)); |
---|
312 |
propvalue = get(LabelObjs(eachText), prop); |
---|
313 |
if ~isstr(propvalue) |
---|
314 |
[n,m] = size(propvalue); |
---|
315 |
str = '['; |
---|
316 |
for i=1:n |
---|
317 |
if i>1 |
---|
318 |
str = [str, sprintf('\t\t')]; |
---|
319 |
end |
---|
320 |
for j=1:m |
---|
321 |
str = [str, sprintf('%s,', num2str(propvalue(i,j)))]; |
---|
322 |
end |
---|
323 |
if i<n |
---|
324 |
str = [str, sprintf(';...\n')]; |
---|
325 |
else |
---|
326 |
str = [str, ']' ]; |
---|
327 |
end |
---|
328 |
end |
---|
329 |
propstr = [str]; |
---|
330 |
else |
---|
331 |
propstr = ['''' propvalue '''']; |
---|
332 |
end |
---|
333 |
printstr = ['set(' Hndlstr 'Hndl, ''' prop ''', ' propstr ');']; |
---|
334 |
fprintf(fid, '%s\n', printstr); |
---|
335 |
end |
---|
336 |
|
---|
337 |
fprintf(fid, '\n'); % blank line separator |
---|
338 |
|
---|
339 |
end |
---|
340 |
|
---|
341 |
% now write commands that set the axes title and labels to contain |
---|
342 |
% the new handles of the text objects just created |
---|
343 |
fprintf(fid, '%s\n', 'set(B, ''Title'', TitleHndl);'); |
---|
344 |
fprintf(fid, '%s\n', 'set(B, ''Xlabel'', XlabelHndl);'); |
---|
345 |
fprintf(fid, '%s\n', 'set(B, ''Ylabel'', YlabelHndl);'); |
---|
346 |
fprintf(fid, '%s\n', 'set(B, ''Zlabel'', ZlabelHndl);'); |
---|
347 |
|
---|
348 |
fprintf(fid, '\n'); % blank line separator |
---|
349 |
|
---|
350 |
% now write commands to set all the text children and their properties |
---|
351 |
for eachText=1:length(TextObjs) |
---|
352 |
fprintf(fid, '%s\n', ['C = text;']); |
---|
353 |
[Ntextprop, Mtextprop] = size(textprop); |
---|
354 |
for element=1:Ntextprop |
---|
355 |
prop = deblank(textprop(element,:)); |
---|
356 |
propvalue = get(TextObjs(eachText), prop); |
---|
357 |
if ~isstr(propvalue) |
---|
358 |
[n,m] = size(propvalue); |
---|
359 |
str = '['; |
---|
360 |
for i=1:n |
---|
361 |
if i>1 |
---|
362 |
str = [str, sprintf('\t\t')]; |
---|
363 |
end |
---|
364 |
for j=1:m |
---|
365 |
str = [str, sprintf('%s,', num2str(propvalue(i,j)))]; |
---|
366 |
end |
---|
367 |
if i<n |
---|
368 |
str = [str, sprintf(';...\n')]; |
---|
369 |
else |
---|
370 |
str = [str, ']' ]; |
---|
371 |
end |
---|
372 |
end |
---|
373 |
propstr = [str]; |
---|
374 |
else |
---|
375 |
propstr = ['''' propvalue '''']; |
---|
376 |
end |
---|
377 |
printstr = ['set(C, ''' prop ''', ' propstr ');']; |
---|
378 |
fprintf(fid, '%s\n', printstr); |
---|
379 |
end |
---|
380 |
|
---|
381 |
fprintf(fid, '\n'); % blank line separator |
---|
382 |
|
---|
383 |
end |
---|
384 |
|
---|
385 |
end % for each axis |
---|
386 |
|
---|
387 |
fclose(fid); |
---|
388 |
|
---|
389 |
% restore pointer |
---|
390 |
if ~isnan(curfig), |
---|
391 |
set(curfig,'Pointer','arrow'); |
---|
392 |
end |
---|