-
Notifications
You must be signed in to change notification settings - Fork 1
/
exportfig.m
68 lines (62 loc) · 2.04 KB
/
exportfig.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
function exportfig(hfig, varargin)
% SYNTAX
% exportfig(hfig)
% exportfig(hfig, Parameter, Value, ...)
%
% DESCRIPTION
% Exports figure displays to various file formats
%
% OPTIONS
% 'name' <string> [current figure name]
% name of file (sans extension)
% 'resolution' <int> [150]
% dpi of output (only applicable for image formats)
% 'formats' <cell> {'png'}
% output formats. valid options are those supported by matlab - e.g.
% a typical parameter value would be:
% {'fig', 'pdf', 'png'}
% 'renderer' <string> ['painters']
% default renderer for export engine.
%
% opts = getopts(varargin);
%
% sName = parseopts('name', opts, get(hfig, 'name'));
% iRes = parseopts('resolution', opts, 150);
% cFormats = parseopts('formats', opts, {'png'});
% sRenderer = parseopts('renderer', opts, 'painters');
opts.name = get(hfig, 'name');
opts.res = 150;
opts.formats = {'png'};
opts.renderer = 'painters';
opts = pvargs(opts, varargin);
sName = opts.name;
iRes = opts.res;
cFormats = opts.formats;
sRenderer = opts.renderer;
if min(size(hfig)) == 1 && length(size(hfig)) == 2,
if iscell(cFormats) && ~isempty(cFormats),
for i = 1:length(hfig),
if iscell(sName),
sCurrName = sName{i};
else
sCurrName = sName;
end;
for j = 1:length(cFormats),
if strcmpi(cFormats{j}, 'fig'),
saveas(hfig(i), sCurrName, 'fig');
else
print( ['-f' num2str(hfig(i))], ...
['-' sRenderer], ...
['-r' num2str(iRes)], ...
['-d' cFormats{j}], ...
sCurrName ...
)
end
end
end
else
error('output formats incorrectly specified');
end
else
error('figure handle must be a scalar or 1D vector');
end