-
Notifications
You must be signed in to change notification settings - Fork 5
/
printstandard.m
60 lines (49 loc) · 1.53 KB
/
printstandard.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
% Print a figure as PNG and EPS, trim whitespace post-print. We use -depsc2
% which provides nice cropped white space outputs by default, and use
% ImageMagick's convert function to trim PNGs similarly.
%
% INPUT:
% fnbase - path and filename (with or without extension - we strip)
%
% NAMED INPUTS:
% F gcf figure handle
% r 600 resolution (dpi)
% formats {'eps'} print outputs - can be 'eps', 'png' or both in cell
% forcepainters 0 always print with painters regardless of renderer
% loose 0 add the -loose flag go depsc2 print
%
% printstandard(fnbase,varargin)
function printstandard(fnbase,varargin);
getArgs(varargin,{'F',gcf,'formats',{'eps'},...
'r',600,'forcepainters',0,'loose',false});
formstruct = struct('png','-dpng','eps','-depsc2');
% Make sure no extension
fnbase = stripextension(fnbase);
if ~isnumeric(F)
F = F.Number;
end
res = sprintf('-r%d',r);
hand = sprintf('-f%d',F);
% Ideally 'none' but this doesn't work with painters
set(gcf,'color',[1 1 1]);
args = {hand,[],res,'-noui'};
if forcepainters
args(end+1) = {'-painters'};
end
if loose
args(end+1) = {'-loose'};
end
if ~iscell(formats)
formats = {formats};
end
for f = formats(:)'
fstr = f{1};
assert(isfield(formstruct,fstr),'format: %s is not supported',fstr);
args{2} = formstruct.(fstr);
print([fnbase '.' fstr],args{:});
end
% Trim whitespace
if any(strcmp(formats,'png'))
system(sprintf('convert -density %d %s -trim %s',r,[fnbase '.png'],...
[fnbase '.png']));
end