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 (2013-2014), Franco Pestilli, Stanford University, pestillifranco@gmail.com.
0001 function figDir = feSavefig(h,varargin) 0002 % Saves a figure for publishing purpose. 0003 % 0004 % function figDir = savefig(h,varargin) 0005 % 0006 % INPUTS: Must be pairs, e.g., 'name',value 0007 % 0008 % figName - the name of the figure file. 0009 % Default: sprintf('feFig_%s',get(h,'Name')) 0010 % figDir - Name of the subfolder where to save this figure. 0011 % Default: current dir ('.') 0012 % figType - fig, eps, jpg, png 0013 % Default: 'png' fastest smallest file, low resolution. 0014 % verbose - display on screen the print command being invoked. 0015 % 0016 % NOTES: 0017 % This function invokes a series of print commands: 0018 % print(h, '-cmyk', '-painters','-depsc2','-tiff','-r500', '-noui', 'figureName') 0019 % 0020 % EXAMPLE: 0021 % feSavefig(figureHandle,'verbose','yes','figName','myfig','figDir','/path/to/fig/folder/'); 0022 % 0023 % 0024 % Copyright (2013-2014), Franco Pestilli, Stanford University, pestillifranco@gmail.com. 0025 0026 % set up default variables: 0027 figName = sprintf('feFig_%s',get(h,'Name')); % the name of the figure file 0028 figDir = '.'; % the subfolder where to save this figure 0029 figType = 'png'; 0030 verbose = 'yes'; % 'yes', 'no', display on screen what it is going on 0031 0032 if ~isempty(varargin) 0033 if mod(length(varargin),2), error('varargin must be pairs'); end 0034 for ii=1:2:(length(varargin)-1) 0035 eval(sprintf('%s = ''%s'';',varargin{ii}, varargin{ii+1})); 0036 end 0037 end 0038 0039 % make the figure dir if it does not exist: 0040 if ~isdir(figDir), mkdir(figDir);end 0041 0042 % Create a print command that will save the figure 0043 switch figType 0044 case {'png'} 0045 printCommand = ... 0046 sprintf('print(%s, ''-painters'',''-dpng'', ''-noui'', ''%s'')', ... 0047 num2str(h),fullfile(figDir,figName)); 0048 0049 case {'jpg'} 0050 printCommand = ... 0051 sprintf('print(%s, ''-djpeg95'',''-r500'', ''-noui'', ''%s'')', ... 0052 num2str(h),fullfile(figDir,figName)); 0053 0054 case {'eps'} 0055 printCommand = ... 0056 sprintf('print(%s, ''-cmyk'', ''-painters'',''-depsc2'',''-tiff'',''-r500'' , ''-noui'', ''%s'')', ... 0057 num2str(h),fullfile(figDir,figName)); 0058 0059 case {'fig'} 0060 printCommand = ... 0061 sprintf('saveas(%s, ''%s'', ''fig'')', num2str(h),figName); 0062 0063 otherwise 0064 error('[%s] Cannot save figure with type set to: %s', mfilename,figType) 0065 end 0066 0067 if strcmpi(verbose,'yes') 0068 fprintf('[%s] Saving eps figure %s/%s\nUsing command: \n%s...\n', ... 0069 mfilename,figDir,figName,printCommand); 0070 end 0071 0072 % Do the printing here: 0073 eval(printCommand); 0074 0075 % Delete output if it was nto requested 0076 if (nargout < 1), clear figDir;end 0077 return 0078