advexpfig

PURPOSE ^

ADVEXPFIG Exports a figure into a file (all internal formats are

SYNOPSIS ^

function [result,msg] = advexpfig(fh,fName,format,varargin)

DESCRIPTION ^

ADVEXPFIG Exports a figure into a file (all internal formats are
   supported as well as some formats generated indirectly using an
   intermediary eps format.

   [result,msg] = ADVAXPFIG(fh,fName,format,varargin)
   Creates an output from the figure fg.

   - fh:           figure handle to export
   - fName:        name of the file to create (if the extension is not
                   given it will be appended automatically according to
                   the format used)
   - format:       output format: all formats that are available for the
                   print command (e.g.:'-dbmpmono', '-dbmp16m',... ; see
                   the help of the print function) plus some additional
                   "indirect" formats which uses eps format as the
                   intermediary format:
                   'jpg<-eps', 'pdf<-eps', 'png<-eps', 'tiff<-eps'
                   The cons of these intermediary formats are that figures
                   have nicer look while the resulting figures have sizes
                   dictated by the bounding box from the intermediary eps
                   figure and not by the paper size.
                   This is especialy suited for LaTeX users for example to
                   use these figure when creating documents,... But, to
                   use these intermediary formats, GhostScript must be
                   installed (se the 'gs' parameter in varargin) as well
                   as the EPS2XXX function must be available.
   - varargin:    (optional) property-value pairs of additional
                   switches/settings (note that they must come in PAIRS):
                   'ui':       can be 'on' (default) if ui objects are to be
                               exported as well or 'off' if not
                   'w':        changed width of the figure in cm (e.g. 15)
                   'h':        changed height of the figure in cm (e.g. 9)
                   'res':      resolution in dpi; given as a number;
                               defaults to whatever is set to be default
                               in the Matlab's print function
                   'colored':  1 or 0; applicable to indirect formats only
                   'renderer': '-painters', '-opengl, '-zbuffer'; if not
                               used, the current renderer is used
                   'gs':       path to the GhostScript executable; used
                               for the formats generated indirectly;
                               defaults to 'gswin32c.exe' in win32 and to
                               'gs' in other platforms; if the executable
                               is not in the system's path, the full path
                               should be given

   - result:       (optional); -1: errors, no file created; 0: file(s) created but
                   there were warnings; 1: OK (no errors or warnings)
   - msg:          (optional); resulting status on file being processed (confirmation string , error
                   string or warning string)

   Examples:
       advexpfig(gcf,'test.eps','-deps2c','w',15,'h',9)

   Notes: for formats generated indirectly this function uses the function
   EPS2XXX. If result and msg are given, no error is raised as this
   variables holds the status.

   See also: EPS2XXX

   Primoz Cermelj, 07.10.2004
   (c) Primoz Cermelj, primoz.cermelj@email.si
   Last revision: 24.11.2005
--------------------------------------------------------------------------

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function [result,msg] = advexpfig(fh,fName,format,varargin)
0002 %ADVEXPFIG Exports a figure into a file (all internal formats are
0003 %   supported as well as some formats generated indirectly using an
0004 %   intermediary eps format.
0005 %
0006 %   [result,msg] = ADVAXPFIG(fh,fName,format,varargin)
0007 %   Creates an output from the figure fg.
0008 %
0009 %   - fh:           figure handle to export
0010 %   - fName:        name of the file to create (if the extension is not
0011 %                   given it will be appended automatically according to
0012 %                   the format used)
0013 %   - format:       output format: all formats that are available for the
0014 %                   print command (e.g.:'-dbmpmono', '-dbmp16m',... ; see
0015 %                   the help of the print function) plus some additional
0016 %                   "indirect" formats which uses eps format as the
0017 %                   intermediary format:
0018 %                   'jpg<-eps', 'pdf<-eps', 'png<-eps', 'tiff<-eps'
0019 %                   The cons of these intermediary formats are that figures
0020 %                   have nicer look while the resulting figures have sizes
0021 %                   dictated by the bounding box from the intermediary eps
0022 %                   figure and not by the paper size.
0023 %                   This is especialy suited for LaTeX users for example to
0024 %                   use these figure when creating documents,... But, to
0025 %                   use these intermediary formats, GhostScript must be
0026 %                   installed (se the 'gs' parameter in varargin) as well
0027 %                   as the EPS2XXX function must be available.
0028 %   - varargin:    (optional) property-value pairs of additional
0029 %                   switches/settings (note that they must come in PAIRS):
0030 %                   'ui':       can be 'on' (default) if ui objects are to be
0031 %                               exported as well or 'off' if not
0032 %                   'w':        changed width of the figure in cm (e.g. 15)
0033 %                   'h':        changed height of the figure in cm (e.g. 9)
0034 %                   'res':      resolution in dpi; given as a number;
0035 %                               defaults to whatever is set to be default
0036 %                               in the Matlab's print function
0037 %                   'colored':  1 or 0; applicable to indirect formats only
0038 %                   'renderer': '-painters', '-opengl, '-zbuffer'; if not
0039 %                               used, the current renderer is used
0040 %                   'gs':       path to the GhostScript executable; used
0041 %                               for the formats generated indirectly;
0042 %                               defaults to 'gswin32c.exe' in win32 and to
0043 %                               'gs' in other platforms; if the executable
0044 %                               is not in the system's path, the full path
0045 %                               should be given
0046 %
0047 %   - result:       (optional); -1: errors, no file created; 0: file(s) created but
0048 %                   there were warnings; 1: OK (no errors or warnings)
0049 %   - msg:          (optional); resulting status on file being processed (confirmation string , error
0050 %                   string or warning string)
0051 %
0052 %   Examples:
0053 %       advexpfig(gcf,'test.eps','-deps2c','w',15,'h',9)
0054 %
0055 %   Notes: for formats generated indirectly this function uses the function
0056 %   EPS2XXX. If result and msg are given, no error is raised as this
0057 %   variables holds the status.
0058 %
0059 %   See also: EPS2XXX
0060 %
0061 %   Primoz Cermelj, 07.10.2004
0062 %   (c) Primoz Cermelj, primoz.cermelj@email.si
0063 %   Last revision: 24.11.2005
0064 %--------------------------------------------------------------------------
0065 if nargin < 3
0066     error('Not enough parametrs given (at least 3 required)');
0067 end
0068 if ~ischar(format)
0069     error('Format must be given as a string');
0070 end
0071 [w,h,gs,colored,optStr] = setoptions(varargin);
0072 
0073 units = get(fh,'Units');
0074 paperMode = get(fh,'PaperPositionMode');
0075 set(fh,'PaperPositionMode','auto');
0076 set(fh,'Units','centimeters');
0077 pos = get(fh,'Position');
0078 oldPos = pos;
0079 newPos = pos;
0080 
0081 % Change the size of the figure
0082 if ~isempty(w)
0083     newPos(3) = w;
0084 end
0085 if ~isempty(h)
0086     newPos(4) = h;
0087 end
0088 set(fh,'Position',newPos);
0089 
0090 % Set the intermediary file name and format if required
0091 if format(1) ~= '-'
0092     % intermediary format
0093     if colored
0094         gformat = '-deps2c';
0095     else
0096         gformat = '-deps2';
0097     end
0098     [pathstr,sourceName,ext] = fileparts(fName);
0099     if isempty(ext)
0100         ext = '.eps~';
0101     else
0102         ext = [ext '~'];
0103     end
0104     if isempty(pathstr)
0105         pathstr = cd;
0106     end
0107     fName = fullfile(pathstr,[sourceName ext]);
0108     intermediary = 1;
0109     switch lower(format)
0110         case 'jpg<-eps'
0111             iformat = 'jpeg';
0112         case 'pdf<-eps'
0113             iformat = 'pdf';
0114         case 'png<-eps'
0115             iformat = 'png';
0116         case 'tiff<-eps'
0117             iformat = 'tiff';
0118         otherwise
0119             error(['Unknown intermediary format: ' forma]);
0120     end
0121 else
0122     gformat = format;
0123     intermediary = 0;
0124 end
0125 
0126 % Print the figure to a file
0127 figure(fh),
0128 eval(['print(''',strtrim(gformat),'''', optStr ',fName)']);
0129 
0130 % Restore the original figure
0131 set(fh,'Position',oldPos);
0132 set(fh,'Units',units);
0133 set(fh,'PaperPositionMode',paperMode);
0134 
0135 result = 1;
0136 msg = '';
0137 
0138 % If intermediary format is used, convert the figure appropriately
0139 if intermediary
0140     [res,resMsg] = eps2xxx(fName,cellstr(iformat),gs);
0141     % Delete the intermediary-format file
0142     if exist(fName)
0143         delete(fName);
0144     end
0145     result = res;
0146     msg = resMsg;
0147     if ~nargout
0148         if res < 0   % error
0149             error(['File not created :' resMsg]);
0150         end
0151         if res == 0  % warning
0152             warning(['Warning :' resMsg]);
0153         end
0154     end
0155 end
0156 
0157 
0158 
0159 %-----------------------------------------------------------------
0160 function [w,h,gs,colored,optStr] = setoptions(varargin)
0161 % Gets the w, h, gs string and the options string from the varargin
0162 % parameters.
0163 
0164 %Defaults
0165 w = [];
0166 h = [];
0167 gs = [];
0168 optStr = '';
0169 colored = 0;
0170 if isempty(varargin{:})
0171     return
0172 end
0173 
0174 % Get the options from the varargin
0175 n = length(varargin{:});
0176 if mod(n,2)~= 0
0177     error('Additional parameters must be given in property-value pairs');
0178 end
0179 for ii=1:2:n-1
0180     prop = varargin{:}{ii};
0181     value = varargin{:}{ii+1};
0182     if ~ischar(prop)
0183         error('Property name must be given as a string');
0184     end
0185     switch lower(prop)
0186         case 'ui'
0187             if strcmpi(value,'off')
0188                 optStr = [optStr ',''-noui'''];
0189             end
0190         case 'w'
0191             w = value;
0192         case 'h'
0193             h = value;
0194         case 'res'
0195             optStr = [optStr ',''' '-r' num2str(value) ''''];
0196         case 'renderer'
0197             if ~isempty(value)
0198                 optStr = [optStr ',''' value ''''];
0199             end
0200         case 'gs'
0201             gs = value;
0202         case 'colored'
0203             colored = value > 0;
0204         otherwise
0205             error('Wrong property given')
0206     end
0207 end

Community support and wiki are available on Redmine. Last update: 18-Apr-2019.