forked from brain-life/encode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
feSavefig.m
77 lines (66 loc) · 2.44 KB
/
feSavefig.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
function figDir = feSavefig(h,varargin)
% Saves a figure for publishing purpose.
%
% function figDir = savefig(h,varargin)
%
% INPUTS: Must be pairs, e.g., 'name',value
%
% figName - the name of the figure file.
% Default: sprintf('feFig_%s',get(h,'Name'))
% figDir - Name of the subfolder where to save this figure.
% Default: current dir ('.')
% figType - fig, eps, jpg, png
% Default: 'png' fastest smallest file, low resolution.
% verbose - display on screen the print command being invoked.
%
% NOTES:
% This function invokes a series of print commands:
% print(h, '-cmyk', '-painters','-depsc2','-tiff','-r500', '-noui', 'figureName')
%
% EXAMPLE:
% feSavefig(figureHandle,'verbose','yes','figName','myfig','figDir','/path/to/fig/folder/');
%
%
% Copyright (2016), Franco Pestilli, Indiana University, [email protected].
% set up default variables:
figName = sprintf('feFig_%s',get(h,'Name')); % the name of the figure file
figDir = '.'; % the subfolder where to save this figure
figType = 'png';
verbose = 'yes'; % 'yes', 'no', display on screen what it is going on
if ~isempty(varargin)
if mod(length(varargin),2), error('varargin must be pairs'); end
for ii=1:2:(length(varargin)-1)
eval(sprintf('%s = ''%s'';',varargin{ii}, varargin{ii+1}));
end
end
% make the figure dir if it does not exist:
if ~isdir(figDir), mkdir(figDir);end
% Create a print command that will save the figure
switch figType
case {'png'}
printCommand = ...
sprintf('print(%s, ''-painters'',''-dpng'', ''-noui'', ''%s'')', ...
num2str(h),fullfile(figDir,figName));
case {'jpg'}
printCommand = ...
sprintf('print(%s, ''-djpeg95'',''-r500'', ''-noui'', ''%s'')', ...
num2str(h),fullfile(figDir,figName));
case {'eps'}
printCommand = ...
sprintf('print(%s, ''-cmyk'', ''-painters'',''-depsc2'',''-tiff'',''-r500'' , ''-noui'', ''%s'')', ...
num2str(h),fullfile(figDir,figName));
case {'fig'}
printCommand = ...
sprintf('saveas(%s, ''%s'', ''fig'')', num2str(h),figName);
otherwise
error('[%s] Cannot save figure with type set to: %s', mfilename,figType)
end
if strcmpi(verbose,'yes')
fprintf('[%s] Saving eps figure %s/%s\nUsing command: \n%s...\n', ...
mfilename,figDir,figName,printCommand);
end
% Do the printing here:
eval(printCommand);
% Delete output if it was nto requested
if (nargout < 1), clear figDir;end
return