-
Notifications
You must be signed in to change notification settings - Fork 14
/
fig2file.m
332 lines (152 loc) · 7.92 KB
/
fig2file.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
function fig2file(varargin)
%% function fig2file.m v.2.1.beta contribution of Riccardo Meldolesi <rmeldolesi@swri.org>
% v.2.0.beta Salem Benramdane <benramdane@ecole-navale.fr>
% routine to save figures into files in different formats fig, eps, jpg & ai
% provided as is. send complaints to dev@null.org
% please send incremented versions and bug reports to benramdane@ecole-navale.fr
% input: fig_handle (optional): figure handle (ex: gcf, 1, myfig..etc) or name (ex: 'toto')
% file_name (optional): figure file name (char type => between '') (ex:'myfigname'...etc)
% i (optional): default i=0 (ypu may change it) (see table below)
% usage: ex1: fig2file(gcf,'toto',1) : saves current figure(gcf) into file 'toto.fig'
% ex2: fig2file(1,'toto') : saves figure 1 into specified default file formats, here: 'toto.jpg'
% ex3: fig2file('synchro') : saves figure named "synchro" into specified default file formats, here: 'toto.jpg'
% ex4: fig2file(1,2) : saves figure 1 into files 'Figure_1.fig' and 'Figure_1.eps'
% ex5: fig2file(0) : saves the current figure named "chirp" into default specified file type
% ex6: fig2file : saves current figure into default file format 'Figure_1.jpg' (provided figure number is "1" with no figure name)
% ex7: fig2file all : saves all the current figures with either "figure_name" or "figure_number" as the file_name with default
% extension
% ex8: fig2file toto : saves current figure into file 'toto.jpg'
% ex9: fig2file(gcf,'toto',{'jpg'}) : saves current figure into file 'toto.jpg'
% ex10: fig2file(gcf,'toto',{'jpg' ; 'pdf'}) : saves current figure into file 'toto.jpg' and 'toto.pdf'
% ex11: fig2file(gcf,{'jpg' ; 'pdf'}) : saves current figure named "chirp" into file 'chirp.jpg' and 'chirp.pdf'
% ex12: fig2file({'jpg' ; 'pdf'}) : saves current figure named "chirp" into file 'chirp.jpg' and 'chirp.pdf'
%
%
% modification: you can freely add/modify the formats you want to be affected to different optional argument
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% i % saved figure
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 4 % fig, eps, jpg, ai
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 3 % fig, eps, jpg
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 2 % fig, eps
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 1 % fig
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 0 % default
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 10 % AI
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
switch nargin
case 3 % ### all arguments entered
fct_saveas(varargin{:});
case 2 % ### only two arguments entered
%==> we consider either:
% "all" and "i" OR
% "fig_handle" and "file_name" OR
% "fig_handle" and "i"
if ischar(varargin{1}) % if the first argument is character => "all":
switch upper(varargin{1})
case 'ALL' % if the argument is "all"
iteratePrint(varargin{2}); % save option "i" is the second argument
return
otherwise
error('ErrorTests:convertTest','argument error! first argument "all" expected, \n Or if you entered figure name, please re-enter figure number instead', varargin{1},'found')
end
elseif ischar(varargin{2}) % if the second argument is character => "file_name":
fct_saveas(varargin{1},varargin{2},0);
elseif isreal(varargin{2}) || iscell(varargin{2}) % if the second argument is "i" => no file_name entered
fct_saveas(varargin{1},getFileName(varargin{1}),varargin{2});
else
error('argument error! type "help fig2file" for more details')
end
case 1 % ### only one argument entered
if ischar(varargin{1}) % if the argument is a string:
%==> supposed (string): "file_name" or 'all'
if strcmpi(varargin{1},'all') % if the argument is "all"
iteratePrint(i);
return
else % if the argument is the file_name
fct_saveas(getCurrentFigureHandle,varargin{1},0);
end
elseif isreal(varargin{1}) || iscell(varargin{1}) % if the argument is integer "i" or a cell array
fct_saveas(getCurrentFigureHandle,getFileName(getCurrentFigureHandle),varargin{1});
else
error('argument error! type "help fig2file" for more details')
end
case 0 % ### no arguments entered
%==> save current figure with figure name as file name and with default options
fct_saveas(getCurrentFigureHandle,getFileName(getCurrentFigureHandle),0);
otherwise
error('argument error! type "help fig2file" for more details')
end
return
% -------------------------------------------
function iteratePrint(printFormatIdentifier)
h = findobj(get(0,'Children'), 'flat','type','figure'); % find all figures
for j=1:length(h) % for all figures
fct_saveas(h(j),getFileName(fig_handle),printFormatIdentifier)
end
return
% -------------------------------------------
function result = getFileName(fig_handle)
result = get(fig_handle,'name'); % set file_name = figure name
if isempty(result) % if no figure name
result=(['Figure_',int2str(fig_handle)]); % set file_name = figure number
end
return
% -------------------------------------------
function result = getCurrentFigureHandle
result = get(0,'CurrentFigure'); % find current fig handles (don't create one if no figures found)
if isempty(result) % if no figure found
error('no figure found')
end
% -------------------------------------------
function fct_saveas(fig_handle,file_name,printFormat)
if ~ishandle(fig_handle) % if handle is invalid
fig_handle = findobj(get(0,'children'),'flat','name',fig_handle);
if isempty(fig_handle)
error('invalid figure handle')
end
end
if isreal(printFormat)
printFormat = processFormat(printFormat);
end
for k = 1:length(printFormat(:))
switch upper(printFormat{k})
case 'FIG'
saveas(fig_handle,sprintf('%s',file_name),'fig');
case 'JPG'
saveas(fig_handle,sprintf('%s',file_name),'jpeg');
case 'EPS'
print(fig_handle,'-dpsc2', sprintf('%s.eps',file_name));
case 'AI'
saveas(fig_handle,sprintf('%s',file_name),'ai');
case 'PDF'
print(fig_handle,'-dpdf',sprintf('%s',file_name));
otherwise
error('unregognised printing option')
end
end
% -------------------------------------------
function printFormat = processFormat(printFormat)
if isreal(printFormat)
switch printFormat
case 0
printFormat = {'jpg'};
case 1
printFormat = {'fig'};
case 2
printFormat = {'fig';'eps'};
case 3
printFormat = {'fig';'jpg';'eps'};
case 4
printFormat = {'fig';'jpg';'eps';'ai'};
case 10
printFormat = {'pdf'};
otherwise
error('input argument printFormat must be between 0 < printFormat <= 4')
end
end