Home > data-vis > supporting_functions > export_fig > export_fig.m

export_fig

PURPOSE ^

EXPORT_FIG Exports figures in a publication-quality format

SYNOPSIS ^

function [imageData, alpha] = export_fig(varargin)

DESCRIPTION ^

EXPORT_FIG  Exports figures in a publication-quality format

 Examples:
   imageData = export_fig
   [imageData, alpha] = export_fig
   export_fig filename
   export_fig filename -format1 -format2
   export_fig ... -nocrop
   export_fig ... -c[<val>,<val>,<val>,<val>]
   export_fig ... -transparent
   export_fig ... -native
   export_fig ... -m<val>
   export_fig ... -r<val>
   export_fig ... -a<val>
   export_fig ... -q<val>
   export_fig ... -p<val>
   export_fig ... -d<gs_option>
   export_fig ... -depsc
   export_fig ... -<renderer>
   export_fig ... -<colorspace>
   export_fig ... -append
   export_fig ... -bookmark
   export_fig ... -clipboard
   export_fig ... -update
   export_fig ... -nofontswap
   export_fig(..., handle)

 This function saves a figure or single axes to one or more vector and/or
 bitmap file formats, and/or outputs a rasterized version to the workspace,
 with the following properties:
   - Figure/axes reproduced as it appears on screen
   - Cropped borders (optional)
   - Embedded fonts (vector formats)
   - Improved line and grid line styles
   - Anti-aliased graphics (bitmap formats)
   - Render images at native resolution (optional for bitmap formats)
   - Transparent background supported (pdf, eps, png, tif)
   - Semi-transparent patch objects supported (png & tif only)
   - RGB, CMYK or grayscale output (CMYK only with pdf, eps, tiff)
   - Variable image compression, including lossless (pdf, eps, jpg)
   - Optionally append to file (pdf, tiff)
   - Vector formats: pdf, eps
   - Bitmap formats: png, tiff, jpg, bmp, export to workspace

 This function is especially suited to exporting figures for use in
 publications and presentations, because of the high quality and
 portability of media produced.

 Note that the background color and figure dimensions are reproduced
 (the latter approximately, and ignoring cropping & magnification) in the
 output file. For transparent background (and semi-transparent patch
 objects), use the -transparent option or set the figure 'Color' property
 to 'none'. To make axes transparent set the axes 'Color' property to
 'none'. PDF, EPS, TIF & PNG are the only formats that support a transparent
 background; only TIF & PNG formats support transparency of patch objects.

 The choice of renderer (opengl, zbuffer or painters) has a large impact
 on the quality of output. The default value (opengl for bitmaps, painters
 for vector formats) generally gives good results, but if you aren't
 satisfied then try another renderer.  Notes: 1) For vector formats (EPS,
 PDF), only painters generates vector graphics. 2) For bitmaps, only
 opengl can render transparent patch objects correctly. 3) For bitmaps,
 only painters will correctly scale line dash and dot lengths when
 magnifying or anti-aliasing. 4) Fonts may be substitued with Courier when
 using painters.

 When exporting to vector format (PDF & EPS) and bitmap format using the
 painters renderer, this function requires that ghostscript is installed
 on your system. You can download this from:
   http://www.ghostscript.com
 When exporting to eps it additionally requires pdftops, from the Xpdf
 suite of functions. You can download this from:
   http://www.foolabs.com/xpdf

 Inputs:
   filename - string containing the name (optionally including full or
              relative path) of the file the figure is to be saved as. If
              a path is not specified, the figure is saved in the current
              directory. If no name and no output arguments are specified,
              the default name, 'export_fig_out', is used. If neither a
              file extension nor a format are specified, a ".png" is added
              and the figure saved in that format.
   -format1, -format2, etc. - strings containing the extensions of the
                              file formats the figure is to be saved as.
                              Valid options are: '-pdf', '-eps', '-png',
                              '-tif', '-jpg' and '-bmp'. All combinations
                              of formats are valid.
   -nocrop - option indicating that the borders of the output are not to
             be cropped.
   -c[<val>,<val>,<val>,<val>] - option indicating crop amounts. Must be
             a 4-element vector of numeric values: [top,right,bottom,left]
             where NaN/Inf indicate auto-cropping, 0 means no cropping,
             and any other value mean cropping in pixel amounts.
   -transparent - option indicating that the figure background is to be
                  made transparent (png, pdf, tif and eps output only).
   -m<val> - option where val indicates the factor to magnify the
             on-screen figure pixel dimensions by when generating bitmap
             outputs (does not affect vector formats). Default: '-m1'.
   -r<val> - option val indicates the resolution (in pixels per inch) to
             export bitmap and vector outputs at, keeping the dimensions
             of the on-screen figure. Default: '-r864' (for vector output
             only). Note that the -m option overides the -r option for
             bitmap outputs only.
   -native - option indicating that the output resolution (when outputting
             a bitmap format) should be such that the vertical resolution
             of the first suitable image found in the figure is at the
             native resolution of that image. To specify a particular
             image to use, give it the tag 'export_fig_native'. Notes:
             This overrides any value set with the -m and -r options. It
             also assumes that the image is displayed front-to-parallel
             with the screen. The output resolution is approximate and
             should not be relied upon. Anti-aliasing can have adverse
             effects on image quality (disable with the -a1 option).
   -a1, -a2, -a3, -a4 - option indicating the amount of anti-aliasing to
                        use for bitmap outputs. '-a1' means no anti-
                        aliasing; '-a4' is the maximum amount (default).
   -<renderer> - option to force a particular renderer (painters, opengl or
                 zbuffer). Default value: opengl for bitmap formats or
                 figures with patches and/or transparent annotations;
                 painters for vector formats without patches/transparencies.
   -<colorspace> - option indicating which colorspace color figures should
                   be saved in: RGB (default), CMYK or gray. CMYK is only
                   supported in pdf, eps and tiff output.
   -q<val> - option to vary bitmap image quality (in pdf, eps and jpg
             files only).  Larger val, in the range 0-100, gives higher
             quality/lower compression. val > 100 gives lossless
             compression. Default: '-q95' for jpg, ghostscript prepress
             default for pdf & eps. Note: lossless compression can
             sometimes give a smaller file size than the default lossy
             compression, depending on the type of images.
   -p<val> - option to pad a border of width val to exported files, where
             val is either a relative size with respect to cropped image
             size (i.e. p=0.01 adds a 1% border). For EPS & PDF formats,
             val can also be integer in units of 1/72" points (abs(val)>1).
             val can be positive (padding) or negative (extra cropping).
             If used, the -nocrop flag will be ignored, i.e. the image will
             always be cropped and then padded. Default: 0 (i.e. no padding).
   -append - option indicating that if the file (pdfs only) already
             exists, the figure is to be appended as a new page, instead
             of being overwritten (default).
   -bookmark - option to indicate that a bookmark with the name of the
               figure is to be created in the output file (pdf only).
   -clipboard - option to save output as an image on the system clipboard.
                Note: background transparency is not preserved in clipboard
   -d<gs_option> - option to indicate a ghostscript setting. For example,
                   -dMaxBitmap=0 or -dNoOutputFonts (Ghostscript 9.15+).
   -depsc -  option to use EPS level-3 rather than the default level-2 print
             device. This solves some bugs with Matlab's default -depsc2 device
             such as discolored subplot lines on images (vector formats only).
   -update - option to download and install the latest version of export_fig
   -nofontswap - option to avoid font swapping. Font swapping is automatically
             done in vector formats (only): 11 standard Matlab fonts are
             replaced by the original figure fonts. This option prevents this.
   handle -  The handle of the figure, axes or uipanels (can be an array of
             handles, but the objects must be in the same figure) to be
             saved. Default: gcf.

 Outputs:
   imageData - MxNxC uint8 image array of the exported image.
   alpha     - MxN single array of alphamatte values in the range [0,1],
               for the case when the background is transparent.

   Some helpful examples and tips can be found at:
      https://github.com/altmany/export_fig

   See also PRINT, SAVEAS, ScreenCapture (on the Matlab File Exchange)

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function [imageData, alpha] = export_fig(varargin)
0002 %EXPORT_FIG  Exports figures in a publication-quality format
0003 %
0004 % Examples:
0005 %   imageData = export_fig
0006 %   [imageData, alpha] = export_fig
0007 %   export_fig filename
0008 %   export_fig filename -format1 -format2
0009 %   export_fig ... -nocrop
0010 %   export_fig ... -c[<val>,<val>,<val>,<val>]
0011 %   export_fig ... -transparent
0012 %   export_fig ... -native
0013 %   export_fig ... -m<val>
0014 %   export_fig ... -r<val>
0015 %   export_fig ... -a<val>
0016 %   export_fig ... -q<val>
0017 %   export_fig ... -p<val>
0018 %   export_fig ... -d<gs_option>
0019 %   export_fig ... -depsc
0020 %   export_fig ... -<renderer>
0021 %   export_fig ... -<colorspace>
0022 %   export_fig ... -append
0023 %   export_fig ... -bookmark
0024 %   export_fig ... -clipboard
0025 %   export_fig ... -update
0026 %   export_fig ... -nofontswap
0027 %   export_fig(..., handle)
0028 %
0029 % This function saves a figure or single axes to one or more vector and/or
0030 % bitmap file formats, and/or outputs a rasterized version to the workspace,
0031 % with the following properties:
0032 %   - Figure/axes reproduced as it appears on screen
0033 %   - Cropped borders (optional)
0034 %   - Embedded fonts (vector formats)
0035 %   - Improved line and grid line styles
0036 %   - Anti-aliased graphics (bitmap formats)
0037 %   - Render images at native resolution (optional for bitmap formats)
0038 %   - Transparent background supported (pdf, eps, png, tif)
0039 %   - Semi-transparent patch objects supported (png & tif only)
0040 %   - RGB, CMYK or grayscale output (CMYK only with pdf, eps, tiff)
0041 %   - Variable image compression, including lossless (pdf, eps, jpg)
0042 %   - Optionally append to file (pdf, tiff)
0043 %   - Vector formats: pdf, eps
0044 %   - Bitmap formats: png, tiff, jpg, bmp, export to workspace
0045 %
0046 % This function is especially suited to exporting figures for use in
0047 % publications and presentations, because of the high quality and
0048 % portability of media produced.
0049 %
0050 % Note that the background color and figure dimensions are reproduced
0051 % (the latter approximately, and ignoring cropping & magnification) in the
0052 % output file. For transparent background (and semi-transparent patch
0053 % objects), use the -transparent option or set the figure 'Color' property
0054 % to 'none'. To make axes transparent set the axes 'Color' property to
0055 % 'none'. PDF, EPS, TIF & PNG are the only formats that support a transparent
0056 % background; only TIF & PNG formats support transparency of patch objects.
0057 %
0058 % The choice of renderer (opengl, zbuffer or painters) has a large impact
0059 % on the quality of output. The default value (opengl for bitmaps, painters
0060 % for vector formats) generally gives good results, but if you aren't
0061 % satisfied then try another renderer.  Notes: 1) For vector formats (EPS,
0062 % PDF), only painters generates vector graphics. 2) For bitmaps, only
0063 % opengl can render transparent patch objects correctly. 3) For bitmaps,
0064 % only painters will correctly scale line dash and dot lengths when
0065 % magnifying or anti-aliasing. 4) Fonts may be substitued with Courier when
0066 % using painters.
0067 %
0068 % When exporting to vector format (PDF & EPS) and bitmap format using the
0069 % painters renderer, this function requires that ghostscript is installed
0070 % on your system. You can download this from:
0071 %   http://www.ghostscript.com
0072 % When exporting to eps it additionally requires pdftops, from the Xpdf
0073 % suite of functions. You can download this from:
0074 %   http://www.foolabs.com/xpdf
0075 %
0076 % Inputs:
0077 %   filename - string containing the name (optionally including full or
0078 %              relative path) of the file the figure is to be saved as. If
0079 %              a path is not specified, the figure is saved in the current
0080 %              directory. If no name and no output arguments are specified,
0081 %              the default name, 'export_fig_out', is used. If neither a
0082 %              file extension nor a format are specified, a ".png" is added
0083 %              and the figure saved in that format.
0084 %   -format1, -format2, etc. - strings containing the extensions of the
0085 %                              file formats the figure is to be saved as.
0086 %                              Valid options are: '-pdf', '-eps', '-png',
0087 %                              '-tif', '-jpg' and '-bmp'. All combinations
0088 %                              of formats are valid.
0089 %   -nocrop - option indicating that the borders of the output are not to
0090 %             be cropped.
0091 %   -c[<val>,<val>,<val>,<val>] - option indicating crop amounts. Must be
0092 %             a 4-element vector of numeric values: [top,right,bottom,left]
0093 %             where NaN/Inf indicate auto-cropping, 0 means no cropping,
0094 %             and any other value mean cropping in pixel amounts.
0095 %   -transparent - option indicating that the figure background is to be
0096 %                  made transparent (png, pdf, tif and eps output only).
0097 %   -m<val> - option where val indicates the factor to magnify the
0098 %             on-screen figure pixel dimensions by when generating bitmap
0099 %             outputs (does not affect vector formats). Default: '-m1'.
0100 %   -r<val> - option val indicates the resolution (in pixels per inch) to
0101 %             export bitmap and vector outputs at, keeping the dimensions
0102 %             of the on-screen figure. Default: '-r864' (for vector output
0103 %             only). Note that the -m option overides the -r option for
0104 %             bitmap outputs only.
0105 %   -native - option indicating that the output resolution (when outputting
0106 %             a bitmap format) should be such that the vertical resolution
0107 %             of the first suitable image found in the figure is at the
0108 %             native resolution of that image. To specify a particular
0109 %             image to use, give it the tag 'export_fig_native'. Notes:
0110 %             This overrides any value set with the -m and -r options. It
0111 %             also assumes that the image is displayed front-to-parallel
0112 %             with the screen. The output resolution is approximate and
0113 %             should not be relied upon. Anti-aliasing can have adverse
0114 %             effects on image quality (disable with the -a1 option).
0115 %   -a1, -a2, -a3, -a4 - option indicating the amount of anti-aliasing to
0116 %                        use for bitmap outputs. '-a1' means no anti-
0117 %                        aliasing; '-a4' is the maximum amount (default).
0118 %   -<renderer> - option to force a particular renderer (painters, opengl or
0119 %                 zbuffer). Default value: opengl for bitmap formats or
0120 %                 figures with patches and/or transparent annotations;
0121 %                 painters for vector formats without patches/transparencies.
0122 %   -<colorspace> - option indicating which colorspace color figures should
0123 %                   be saved in: RGB (default), CMYK or gray. CMYK is only
0124 %                   supported in pdf, eps and tiff output.
0125 %   -q<val> - option to vary bitmap image quality (in pdf, eps and jpg
0126 %             files only).  Larger val, in the range 0-100, gives higher
0127 %             quality/lower compression. val > 100 gives lossless
0128 %             compression. Default: '-q95' for jpg, ghostscript prepress
0129 %             default for pdf & eps. Note: lossless compression can
0130 %             sometimes give a smaller file size than the default lossy
0131 %             compression, depending on the type of images.
0132 %   -p<val> - option to pad a border of width val to exported files, where
0133 %             val is either a relative size with respect to cropped image
0134 %             size (i.e. p=0.01 adds a 1% border). For EPS & PDF formats,
0135 %             val can also be integer in units of 1/72" points (abs(val)>1).
0136 %             val can be positive (padding) or negative (extra cropping).
0137 %             If used, the -nocrop flag will be ignored, i.e. the image will
0138 %             always be cropped and then padded. Default: 0 (i.e. no padding).
0139 %   -append - option indicating that if the file (pdfs only) already
0140 %             exists, the figure is to be appended as a new page, instead
0141 %             of being overwritten (default).
0142 %   -bookmark - option to indicate that a bookmark with the name of the
0143 %               figure is to be created in the output file (pdf only).
0144 %   -clipboard - option to save output as an image on the system clipboard.
0145 %                Note: background transparency is not preserved in clipboard
0146 %   -d<gs_option> - option to indicate a ghostscript setting. For example,
0147 %                   -dMaxBitmap=0 or -dNoOutputFonts (Ghostscript 9.15+).
0148 %   -depsc -  option to use EPS level-3 rather than the default level-2 print
0149 %             device. This solves some bugs with Matlab's default -depsc2 device
0150 %             such as discolored subplot lines on images (vector formats only).
0151 %   -update - option to download and install the latest version of export_fig
0152 %   -nofontswap - option to avoid font swapping. Font swapping is automatically
0153 %             done in vector formats (only): 11 standard Matlab fonts are
0154 %             replaced by the original figure fonts. This option prevents this.
0155 %   handle -  The handle of the figure, axes or uipanels (can be an array of
0156 %             handles, but the objects must be in the same figure) to be
0157 %             saved. Default: gcf.
0158 %
0159 % Outputs:
0160 %   imageData - MxNxC uint8 image array of the exported image.
0161 %   alpha     - MxN single array of alphamatte values in the range [0,1],
0162 %               for the case when the background is transparent.
0163 %
0164 %   Some helpful examples and tips can be found at:
0165 %      https://github.com/altmany/export_fig
0166 %
0167 %   See also PRINT, SAVEAS, ScreenCapture (on the Matlab File Exchange)
0168 
0169 %{
0170 % Copyright (C) Oliver Woodford 2008-2014, Yair Altman 2015-
0171 
0172 % The idea of using ghostscript is inspired by Peder Axensten's SAVEFIG
0173 % (fex id: 10889) which is itself inspired by EPS2PDF (fex id: 5782).
0174 % The idea for using pdftops came from the MATLAB newsgroup (id: 168171).
0175 % The idea of editing the EPS file to change line styles comes from Jiro
0176 % Doke's FIXPSLINESTYLE (fex id: 17928).
0177 % The idea of changing dash length with line width came from comments on
0178 % fex id: 5743, but the implementation is mine :)
0179 % The idea of anti-aliasing bitmaps came from Anders Brun's MYAA (fex id:
0180 % 20979).
0181 % The idea of appending figures in pdfs came from Matt C in comments on the
0182 % FEX (id: 23629)
0183 
0184 % Thanks to Roland Martin for pointing out the colour MATLAB
0185 % bug/feature with colorbar axes and transparent backgrounds.
0186 % Thanks also to Andrew Matthews for describing a bug to do with the figure
0187 % size changing in -nodisplay mode. I couldn't reproduce it, but included a
0188 % fix anyway.
0189 % Thanks to Tammy Threadgill for reporting a bug where an axes is not
0190 % isolated from gui objects.
0191 %}
0192 %{
0193 % 23/02/12: Ensure that axes limits don't change during printing
0194 % 14/03/12: Fix bug in fixing the axes limits (thanks to Tobias Lamour for reporting it).
0195 % 02/05/12: Incorporate patch of Petr Nechaev (many thanks), enabling bookmarking of figures in pdf files.
0196 % 09/05/12: Incorporate patch of Arcelia Arrieta (many thanks), to keep tick marks fixed.
0197 % 12/12/12: Add support for isolating uipanels. Thanks to michael for suggesting it.
0198 % 25/09/13: Add support for changing resolution in vector formats. Thanks to Jan Jaap Meijer for suggesting it.
0199 % 07/05/14: Add support for '~' at start of path. Thanks to Sally Warner for suggesting it.
0200 % 24/02/15: Fix Matlab R2014b bug (issue #34): plot markers are not displayed when ZLimMode='manual'
0201 % 25/02/15: Fix issue #4 (using HG2 on R2014a and earlier)
0202 % 25/02/15: Fix issue #21 (bold TeX axes labels/titles in R2014b)
0203 % 26/02/15: If temp dir is not writable, use the user-specified folder for temporary EPS/PDF files (Javier Paredes)
0204 % 27/02/15: Modified repository URL from github.com/ojwoodford to /altmany
0205 %           Indented main function
0206 %           Added top-level try-catch block to display useful workarounds
0207 % 28/02/15: Enable users to specify optional ghostscript options (issue #36)
0208 % 06/03/15: Improved image padding & cropping thanks to Oscar Hartogensis
0209 % 26/03/15: Fixed issue #49 (bug with transparent grayscale images); fixed out-of-memory issue
0210 % 26/03/15: Fixed issue #42: non-normalized annotations on HG1
0211 % 26/03/15: Fixed issue #46: Ghostscript crash if figure units <> pixels
0212 % 27/03/15: Fixed issue #39: bad export of transparent annotations/patches
0213 % 28/03/15: Fixed issue #50: error on some Matlab versions with the fix for issue #42
0214 % 29/03/15: Fixed issue #33: bugs in Matlab's print() function with -cmyk
0215 % 29/03/15: Improved processing of input args (accept space between param name & value, related to issue #51)
0216 % 30/03/15: When exporting *.fig files, then saveas *.fig if figure is open, otherwise export the specified fig file
0217 % 30/03/15: Fixed edge case bug introduced yesterday (commit #ae1755bd2e11dc4e99b95a7681f6e211b3fa9358)
0218 % 09/04/15: Consolidated header comment sections; initialize output vars only if requested (nargout>0)
0219 % 14/04/15: Workaround for issue #45: lines in image subplots are exported in invalid color
0220 % 15/04/15: Fixed edge-case in parsing input parameters; fixed help section to show the -depsc option (issue #45)
0221 % 21/04/15: Bug fix: Ghostscript croaks on % chars in output PDF file (reported by Sven on FEX page, 15-Jul-2014)
0222 % 22/04/15: Bug fix: Pdftops croaks on relative paths (reported by Tintin Milou on FEX page, 19-Jan-2015)
0223 % 04/05/15: Merged fix #63 (Kevin Mattheus Moerman): prevent tick-label changes during export
0224 % 07/05/15: Partial fix for issue #65: PDF export used painters rather than opengl renderer (thanks Nguyenr)
0225 % 08/05/15: Fixed issue #65: bad PDF append since commit #e9f3cdf 21/04/15 (thanks Robert Nguyen)
0226 % 12/05/15: Fixed issue #67: exponent labels cropped in export, since fix #63 (04/05/15)
0227 % 28/05/15: Fixed issue #69: set non-bold label font only if the string contains symbols (\beta etc.), followup to issue #21
0228 % 29/05/15: Added informative error message in case user requested SVG output (issue #72)
0229 % 09/06/15: Fixed issue #58: -transparent removed anti-aliasing when exporting to PNG
0230 % 19/06/15: Added -update option to download and install the latest version of export_fig
0231 % 07/07/15: Added -nofontswap option to avoid font-swapping in EPS/PDF
0232 % 16/07/15: Fixed problem with anti-aliasing on old Matlab releases
0233 % 11/09/15: Fixed issue #103: magnification must never become negative; also fixed reported error msg in parsing input params
0234 % 26/09/15: Alert if trying to export transparent patches/areas to non-PNG outputs (issue #108)
0235 % 04/10/15: Do not suggest workarounds for certain errors that have already been handled previously
0236 % 01/11/15: Fixed issue #112: use same renderer in print2eps as export_fig (thanks to Jesús Pestana Puerta)
0237 % 10/11/15: Custom GS installation webpage for MacOS. Thanks to Andy Hueni via FEX
0238 % 19/11/15: Fixed clipboard export in R2015b (thanks to Dan K via FEX)
0239 % 21/02/16: Added -c option for indicating specific crop amounts (idea by Cedric Noordam on FEX)
0240 % 08/05/16: Added message about possible error reason when groot.Units~=pixels (issue #149)
0241 % 17/05/16: Fixed case of image YData containing more than 2 elements (issue #151)
0242 % 08/08/16: Enabled exporting transparency to TIF, in addition to PNG/PDF (issue #168)
0243 %}
0244 
0245     if nargout
0246         [imageData, alpha] = deal([]);
0247     end
0248     hadError = false;
0249     displaySuggestedWorkarounds = true;
0250 
0251     % Ensure the figure is rendered correctly _now_ so that properties like axes limits are up-to-date
0252     drawnow;
0253     pause(0.05);  % this solves timing issues with Java Swing's EDT (http://undocumentedmatlab.com/blog/solving-a-matlab-hang-problem)
0254 
0255     % Parse the input arguments
0256     fig = get(0, 'CurrentFigure');
0257     [fig, options] = parse_args(nargout, fig, varargin{:});
0258 
0259     % Ensure that we have a figure handle
0260     if isequal(fig,-1)
0261         return;  % silent bail-out
0262     elseif isempty(fig)
0263         error('No figure found');
0264     end
0265 
0266     % Isolate the subplot, if it is one
0267     cls = all(ismember(get(fig, 'Type'), {'axes', 'uipanel'}));
0268     if cls
0269         % Given handles of one or more axes, so isolate them from the rest
0270         fig = isolate_axes(fig);
0271     else
0272         % Check we have a figure
0273         if ~isequal(get(fig, 'Type'), 'figure');
0274             error('Handle must be that of a figure, axes or uipanel');
0275         end
0276         % Get the old InvertHardcopy mode
0277         old_mode = get(fig, 'InvertHardcopy');
0278     end
0279 
0280     % Hack the font units where necessary (due to a font rendering bug in print?).
0281     % This may not work perfectly in all cases.
0282     % Also it can change the figure layout if reverted, so use a copy.
0283     magnify = options.magnify * options.aa_factor;
0284     if isbitmap(options) && magnify ~= 1
0285         fontu = findall(fig, 'FontUnits', 'normalized');
0286         if ~isempty(fontu)
0287             % Some normalized font units found
0288             if ~cls
0289                 fig = copyfig(fig);
0290                 set(fig, 'Visible', 'off');
0291                 fontu = findall(fig, 'FontUnits', 'normalized');
0292                 cls = true;
0293             end
0294             set(fontu, 'FontUnits', 'points');
0295         end
0296     end
0297 
0298     try
0299         % MATLAB "feature": axes limits and tick marks can change when printing
0300         Hlims = findall(fig, 'Type', 'axes');
0301         if ~cls
0302             % Record the old axes limit and tick modes
0303             Xlims = make_cell(get(Hlims, 'XLimMode'));
0304             Ylims = make_cell(get(Hlims, 'YLimMode'));
0305             Zlims = make_cell(get(Hlims, 'ZLimMode'));
0306             Xtick = make_cell(get(Hlims, 'XTickMode'));
0307             Ytick = make_cell(get(Hlims, 'YTickMode'));
0308             Ztick = make_cell(get(Hlims, 'ZTickMode'));
0309             Xlabel = make_cell(get(Hlims, 'XTickLabelMode')); 
0310             Ylabel = make_cell(get(Hlims, 'YTickLabelMode')); 
0311             Zlabel = make_cell(get(Hlims, 'ZTickLabelMode')); 
0312         end
0313 
0314         % Set all axes limit and tick modes to manual, so the limits and ticks can't change
0315         % Fix Matlab R2014b bug (issue #34): plot markers are not displayed when ZLimMode='manual'
0316         set(Hlims, 'XLimMode', 'manual', 'YLimMode', 'manual');
0317         set_tick_mode(Hlims, 'X');
0318         set_tick_mode(Hlims, 'Y');
0319         if ~using_hg2(fig)
0320             set(Hlims,'ZLimMode', 'manual');
0321             set_tick_mode(Hlims, 'Z');
0322         end
0323     catch
0324         % ignore - fix issue #4 (using HG2 on R2014a and earlier)
0325     end
0326 
0327     % Fix issue #21 (bold TeX axes labels/titles in R2014b when exporting to EPS/PDF)
0328     try
0329         if using_hg2(fig) && isvector(options)
0330             % Set the FontWeight of axes labels/titles to 'normal'
0331             % Fix issue #69: set non-bold font only if the string contains symbols (\beta etc.)
0332             texLabels = findall(fig, 'type','text', 'FontWeight','bold');
0333             symbolIdx = ~cellfun('isempty',strfind({texLabels.String},'\'));
0334             set(texLabels(symbolIdx), 'FontWeight','normal');
0335         end
0336     catch
0337         % ignore
0338     end
0339 
0340     % Fix issue #42: non-normalized annotations on HG1 (internal Matlab bug)
0341     annotationHandles = [];
0342     try
0343         if ~using_hg2(fig)
0344             annotationHandles = findall(fig,'Type','hggroup','-and','-property','Units','-and','-not','Units','norm');
0345             try  % suggested by Jesús Pestana Puerta (jespestana) 30/9/2015
0346                 originalUnits = get(annotationHandles,'Units');
0347                 set(annotationHandles,'Units','norm');
0348             catch
0349             end
0350         end
0351     catch
0352         % should never happen, but ignore in any case - issue #50
0353     end
0354 
0355     % Fix issue #46: Ghostscript crash if figure units <> pixels
0356     oldFigUnits = get(fig,'Units');
0357     set(fig,'Units','pixels');
0358 
0359     % Set to print exactly what is there
0360     set(fig, 'InvertHardcopy', 'off');
0361     % Set the renderer
0362     switch options.renderer
0363         case 1
0364             renderer = '-opengl';
0365         case 2
0366             renderer = '-zbuffer';
0367         case 3
0368             renderer = '-painters';
0369         otherwise
0370             renderer = '-opengl'; % Default for bitmaps
0371     end
0372 
0373     % Handle transparent patches
0374     hasTransparency = ~isempty(findall(fig,'-property','FaceAlpha','-and','-not','FaceAlpha',1));
0375     hasPatches      = ~isempty(findall(fig,'type','patch'));
0376     if hasTransparency
0377         % Alert if trying to export transparent patches/areas to non-supported outputs (issue #108)
0378         % http://www.mathworks.com/matlabcentral/answers/265265-can-export_fig-or-else-draw-vector-graphics-with-transparent-surfaces
0379         % TODO - use transparency when exporting to PDF by not passing via print2eps
0380         msg = 'export_fig currently supports transparent patches/areas only in PNG output. ';
0381         if options.pdf
0382             warning('export_fig:transparency', '%s\nTo export transparent patches/areas to PDF, use the print command:\n print(gcf, ''-dpdf'', ''%s.pdf'');', msg, options.name);
0383         elseif ~options.png && ~options.tif  % issue #168
0384             warning('export_fig:transparency', '%s\nTo export the transparency correctly, try using the ScreenCapture utility on the Matlab File Exchange: http://bit.ly/1QFrBip', msg);
0385         end
0386     end
0387 
0388     try
0389         % Do the bitmap formats first
0390         if isbitmap(options)
0391             if abs(options.bb_padding) > 1
0392                 displaySuggestedWorkarounds = false;
0393                 error('For bitmap output (png,jpg,tif,bmp) the padding value (-p) must be between -1<p<1')
0394             end
0395             % Get the background colour
0396             if options.transparent && (options.png || options.alpha)
0397                 % Get out an alpha channel
0398                 % MATLAB "feature": black colorbar axes can change to white and vice versa!
0399                 hCB = findall(fig, 'Type','axes', 'Tag','Colorbar');
0400                 if isempty(hCB)
0401                     yCol = [];
0402                     xCol = [];
0403                 else
0404                     yCol = get(hCB, 'YColor');
0405                     xCol = get(hCB, 'XColor');
0406                     if iscell(yCol)
0407                         yCol = cell2mat(yCol);
0408                         xCol = cell2mat(xCol);
0409                     end
0410                     yCol = sum(yCol, 2);
0411                     xCol = sum(xCol, 2);
0412                 end
0413                 % MATLAB "feature": apparently figure size can change when changing
0414                 % colour in -nodisplay mode
0415                 pos = get(fig, 'Position');
0416                 % Set the background colour to black, and set size in case it was
0417                 % changed internally
0418                 tcol = get(fig, 'Color');
0419                 set(fig, 'Color', 'k', 'Position', pos);
0420                 % Correct the colorbar axes colours
0421                 set(hCB(yCol==0), 'YColor', [0 0 0]);
0422                 set(hCB(xCol==0), 'XColor', [0 0 0]);
0423 
0424                 % The following code might cause out-of-memory errors
0425                 try
0426                     % Print large version to array
0427                     B = print2array(fig, magnify, renderer);
0428                     % Downscale the image
0429                     B = downsize(single(B), options.aa_factor);
0430                 catch
0431                     % This is more conservative in memory, but kills transparency (issue #58)
0432                     B = single(print2array(fig, magnify/options.aa_factor, renderer));
0433                 end
0434 
0435                 % Set background to white (and set size)
0436                 set(fig, 'Color', 'w', 'Position', pos);
0437                 % Correct the colorbar axes colours
0438                 set(hCB(yCol==3), 'YColor', [1 1 1]);
0439                 set(hCB(xCol==3), 'XColor', [1 1 1]);
0440 
0441                 % The following code might cause out-of-memory errors
0442                 try
0443                     % Print large version to array
0444                     A = print2array(fig, magnify, renderer);
0445                     % Downscale the image
0446                     A = downsize(single(A), options.aa_factor);
0447                 catch
0448                     % This is more conservative in memory, but kills transparency (issue #58)
0449                     A = single(print2array(fig, magnify/options.aa_factor, renderer));
0450                 end
0451 
0452                 % Set the background colour (and size) back to normal
0453                 set(fig, 'Color', tcol, 'Position', pos);
0454                 % Compute the alpha map
0455                 alpha = round(sum(B - A, 3)) / (255 * 3) + 1;
0456                 A = alpha;
0457                 A(A==0) = 1;
0458                 A = B ./ A(:,:,[1 1 1]);
0459                 clear B
0460                 % Convert to greyscale
0461                 if options.colourspace == 2
0462                     A = rgb2grey(A);
0463                 end
0464                 A = uint8(A);
0465                 % Crop the background
0466                 if options.crop
0467                     %[alpha, v] = crop_borders(alpha, 0, 1, options.crop_amounts);
0468                     %A = A(v(1):v(2),v(3):v(4),:);
0469                     [alpha, vA, vB] = crop_borders(alpha, 0, options.bb_padding, options.crop_amounts);
0470                     if ~any(isnan(vB)) % positive padding
0471                         B = repmat(uint8(zeros(1,1,size(A,3))),size(alpha));
0472                         B(vB(1):vB(2), vB(3):vB(4), :) = A(vA(1):vA(2), vA(3):vA(4), :); % ADDED BY OH
0473                         A = B;
0474                     else  % negative padding
0475                         A = A(vA(1):vA(2), vA(3):vA(4), :);
0476                     end
0477                 end
0478                 if options.png
0479                     % Compute the resolution
0480                     res = options.magnify * get(0, 'ScreenPixelsPerInch') / 25.4e-3;
0481                     % Save the png
0482                     imwrite(A, [options.name '.png'], 'Alpha', double(alpha), 'ResolutionUnit', 'meter', 'XResolution', res, 'YResolution', res);
0483                     % Clear the png bit
0484                     options.png = false;
0485                 end
0486                 % Return only one channel for greyscale
0487                 if isbitmap(options)
0488                     A = check_greyscale(A);
0489                 end
0490                 if options.alpha
0491                     % Store the image
0492                     imageData = A;
0493                     % Clear the alpha bit
0494                     options.alpha = false;
0495                 end
0496                 % Get the non-alpha image
0497                 if isbitmap(options)
0498                     alph = alpha(:,:,ones(1, size(A, 3)));
0499                     A = uint8(single(A) .* alph + 255 * (1 - alph));
0500                     clear alph
0501                 end
0502                 if options.im
0503                     % Store the new image
0504                     imageData = A;
0505                 end
0506             else
0507                 % Print large version to array
0508                 if options.transparent
0509                     % MATLAB "feature": apparently figure size can change when changing
0510                     % colour in -nodisplay mode
0511                     pos = get(fig, 'Position');
0512                     tcol = get(fig, 'Color');
0513                     set(fig, 'Color', 'w', 'Position', pos);
0514                     A = print2array(fig, magnify, renderer);
0515                     set(fig, 'Color', tcol, 'Position', pos);
0516                     tcol = 255;
0517                 else
0518                     [A, tcol] = print2array(fig, magnify, renderer);
0519                 end
0520                 % Crop the background
0521                 if options.crop
0522                     A = crop_borders(A, tcol, options.bb_padding, options.crop_amounts);
0523                 end
0524                 % Downscale the image
0525                 A = downsize(A, options.aa_factor);
0526                 if options.colourspace == 2
0527                     % Convert to greyscale
0528                     A = rgb2grey(A);
0529                 else
0530                     % Return only one channel for greyscale
0531                     A = check_greyscale(A);
0532                 end
0533                 % Outputs
0534                 if options.im
0535                     imageData = A;
0536                 end
0537                 if options.alpha
0538                     imageData = A;
0539                     alpha = zeros(size(A, 1), size(A, 2), 'single');
0540                 end
0541             end
0542             % Save the images
0543             if options.png
0544                 res = options.magnify * get(0, 'ScreenPixelsPerInch') / 25.4e-3;
0545                 imwrite(A, [options.name '.png'], 'ResolutionUnit', 'meter', 'XResolution', res, 'YResolution', res);
0546             end
0547             if options.bmp
0548                 imwrite(A, [options.name '.bmp']);
0549             end
0550             % Save jpeg with given quality
0551             if options.jpg
0552                 quality = options.quality;
0553                 if isempty(quality)
0554                     quality = 95;
0555                 end
0556                 if quality > 100
0557                     imwrite(A, [options.name '.jpg'], 'Mode', 'lossless');
0558                 else
0559                     imwrite(A, [options.name '.jpg'], 'Quality', quality);
0560                 end
0561             end
0562             % Save tif images in cmyk if wanted (and possible)
0563             if options.tif
0564                 if options.colourspace == 1 && size(A, 3) == 3
0565                     A = double(255 - A);
0566                     K = min(A, [], 3);
0567                     K_ = 255 ./ max(255 - K, 1);
0568                     C = (A(:,:,1) - K) .* K_;
0569                     M = (A(:,:,2) - K) .* K_;
0570                     Y = (A(:,:,3) - K) .* K_;
0571                     A = uint8(cat(3, C, M, Y, K));
0572                     clear C M Y K K_
0573                 end
0574                 append_mode = {'overwrite', 'append'};
0575                 imwrite(A, [options.name '.tif'], 'Resolution', options.magnify*get(0, 'ScreenPixelsPerInch'), 'WriteMode', append_mode{options.append+1});
0576             end
0577         end
0578 
0579         % Now do the vector formats
0580         if isvector(options)
0581             % Set the default renderer to painters
0582             if ~options.renderer
0583                 if hasTransparency || hasPatches
0584                     % This is *MUCH* slower, but more accurate for patches and transparent annotations (issue #39)
0585                     renderer = '-opengl';
0586                 else
0587                     renderer = '-painters';
0588                 end
0589             end
0590             options.rendererStr = renderer;  % fix for issue #112
0591             % Generate some filenames
0592             tmp_nam = [tempname '.eps'];
0593             try
0594                 % Ensure that the temp dir is writable (Javier Paredes 30/1/15)
0595                 fid = fopen(tmp_nam,'w');
0596                 fwrite(fid,1);
0597                 fclose(fid);
0598                 delete(tmp_nam);
0599                 isTempDirOk = true;
0600             catch
0601                 % Temp dir is not writable, so use the user-specified folder
0602                 [dummy,fname,fext] = fileparts(tmp_nam); %#ok<ASGLU>
0603                 fpath = fileparts(options.name);
0604                 tmp_nam = fullfile(fpath,[fname fext]);
0605                 isTempDirOk = false;
0606             end
0607             if isTempDirOk
0608                 pdf_nam_tmp = [tempname '.pdf'];
0609             else
0610                 pdf_nam_tmp = fullfile(fpath,[fname '.pdf']);
0611             end
0612             if options.pdf
0613                 pdf_nam = [options.name '.pdf'];
0614                 try copyfile(pdf_nam, pdf_nam_tmp, 'f'); catch, end  % fix for issue #65
0615             else
0616                 pdf_nam = pdf_nam_tmp;
0617             end
0618             % Generate the options for print
0619             p2eArgs = {renderer, sprintf('-r%d', options.resolution)};
0620             if options.colourspace == 1  % CMYK
0621                 % Issue #33: due to internal bugs in Matlab's print() function, we can't use its -cmyk option
0622                 %p2eArgs{end+1} = '-cmyk';
0623             end
0624             if ~options.crop
0625                 % Issue #56: due to internal bugs in Matlab's print() function, we can't use its internal cropping mechanism,
0626                 % therefore we always use '-loose' (in print2eps.m) and do our own cropping (in crop_borders)
0627                 %p2eArgs{end+1} = '-loose';
0628             end
0629             if any(strcmpi(varargin,'-depsc'))
0630                 % Issue #45: lines in image subplots are exported in invalid color.
0631                 % The workaround is to use the -depsc parameter instead of the default -depsc2
0632                 p2eArgs{end+1} = '-depsc';
0633             end
0634             try
0635                 % Generate an eps
0636                 print2eps(tmp_nam, fig, options, p2eArgs{:});
0637                 % Remove the background, if desired
0638                 if options.transparent && ~isequal(get(fig, 'Color'), 'none')
0639                     eps_remove_background(tmp_nam, 1 + using_hg2(fig));
0640                 end
0641                 % Fix colorspace to CMYK, if requested (workaround for issue #33)
0642                 if options.colourspace == 1  % CMYK
0643                     % Issue #33: due to internal bugs in Matlab's print() function, we can't use its -cmyk option
0644                     change_rgb_to_cmyk(tmp_nam);
0645                 end
0646                 % Add a bookmark to the PDF if desired
0647                 if options.bookmark
0648                     fig_nam = get(fig, 'Name');
0649                     if isempty(fig_nam)
0650                         warning('export_fig:EmptyBookmark', 'Bookmark requested for figure with no name. Bookmark will be empty.');
0651                     end
0652                     add_bookmark(tmp_nam, fig_nam);
0653                 end
0654                 % Generate a pdf
0655                 eps2pdf(tmp_nam, pdf_nam_tmp, 1, options.append, options.colourspace==2, options.quality, options.gs_options);
0656                 % Ghostscript croaks on % chars in the output PDF file, so use tempname and then rename the file
0657                 try movefile(pdf_nam_tmp, pdf_nam, 'f'); catch, end
0658             catch ex
0659                 % Delete the eps
0660                 delete(tmp_nam);
0661                 rethrow(ex);
0662             end
0663             % Delete the eps
0664             delete(tmp_nam);
0665             if options.eps
0666                 try
0667                     % Generate an eps from the pdf
0668                     % since pdftops can't handle relative paths (e.g., '..\'), use a temp file
0669                     eps_nam_tmp = strrep(pdf_nam_tmp,'.pdf','.eps');
0670                     pdf2eps(pdf_nam, eps_nam_tmp);
0671                     movefile(eps_nam_tmp,  [options.name '.eps'], 'f');
0672                 catch ex
0673                     if ~options.pdf
0674                         % Delete the pdf
0675                         delete(pdf_nam);
0676                     end
0677                     try delete(eps_nam_tmp); catch, end
0678                     rethrow(ex);
0679                 end
0680                 if ~options.pdf
0681                     % Delete the pdf
0682                     delete(pdf_nam);
0683                 end
0684             end
0685         end
0686 
0687         % Revert the figure or close it (if requested)
0688         if cls || options.closeFig
0689             % Close the created figure
0690             close(fig);
0691         else
0692             % Reset the hardcopy mode
0693             set(fig, 'InvertHardcopy', old_mode);
0694             % Reset the axes limit and tick modes
0695             for a = 1:numel(Hlims)
0696                 try
0697                     set(Hlims(a), 'XLimMode', Xlims{a}, 'YLimMode', Ylims{a}, 'ZLimMode', Zlims{a},... 
0698                                   'XTickMode', Xtick{a}, 'YTickMode', Ytick{a}, 'ZTickMode', Ztick{a},...
0699                                   'XTickLabelMode', Xlabel{a}, 'YTickLabelMode', Ylabel{a}, 'ZTickLabelMode', Zlabel{a}); 
0700                 catch
0701                     % ignore - fix issue #4 (using HG2 on R2014a and earlier)
0702                 end
0703             end
0704             % Revert the tex-labels font weights
0705             try set(texLabels, 'FontWeight','bold'); catch, end
0706             % Revert annotation units
0707             for handleIdx = 1 : numel(annotationHandles)
0708                 try
0709                     oldUnits = originalUnits{handleIdx};
0710                 catch
0711                     oldUnits = originalUnits;
0712                 end
0713                 try set(annotationHandles(handleIdx),'Units',oldUnits); catch, end
0714             end
0715             % Revert figure units
0716             set(fig,'Units',oldFigUnits);
0717         end
0718 
0719         % Output to clipboard (if requested)
0720         if options.clipboard
0721             % Delete the output file if unchanged from the default name ('export_fig_out.png')
0722             if strcmpi(options.name,'export_fig_out')
0723                 try
0724                     fileInfo = dir('export_fig_out.png');
0725                     if ~isempty(fileInfo)
0726                         timediff = now - fileInfo.datenum;
0727                         ONE_SEC = 1/24/60/60;
0728                         if timediff < ONE_SEC
0729                             delete('export_fig_out.png');
0730                         end
0731                     end
0732                 catch
0733                     % never mind...
0734                 end
0735             end
0736 
0737             % Save the image in the system clipboard
0738             % credit: Jiro Doke's IMCLIPBOARD: http://www.mathworks.com/matlabcentral/fileexchange/28708-imclipboard
0739             try
0740                 error(javachk('awt', 'export_fig -clipboard output'));
0741             catch
0742                 warning('export_fig -clipboard output failed: requires Java to work');
0743                 return;
0744             end
0745             try
0746                 % Import necessary Java classes
0747                 import java.awt.Toolkit
0748                 import java.awt.image.BufferedImage
0749                 import java.awt.datatransfer.DataFlavor
0750 
0751                 % Get System Clipboard object (java.awt.Toolkit)
0752                 cb = Toolkit.getDefaultToolkit.getSystemClipboard();
0753 
0754                 % Add java class (ImageSelection) to the path
0755                 if ~exist('ImageSelection', 'class')
0756                     javaaddpath(fileparts(which(mfilename)), '-end');
0757                 end
0758 
0759                 % Get image size
0760                 ht = size(imageData, 1);
0761                 wd = size(imageData, 2);
0762 
0763                 % Convert to Blue-Green-Red format
0764                 try
0765                     imageData2 = imageData(:, :, [3 2 1]);
0766                 catch
0767                     % Probably gray-scaled image (2D, without the 3rd [RGB] dimension)
0768                     imageData2 = imageData(:, :, [1 1 1]);
0769                 end
0770 
0771                 % Convert to 3xWxH format
0772                 imageData2 = permute(imageData2, [3, 2, 1]);
0773 
0774                 % Append Alpha data (unused - transparency is not supported in clipboard copy)
0775                 alphaData2 = uint8(permute(255*alpha,[3,2,1])); %=255*ones(1,wd,ht,'uint8')
0776                 imageData2 = cat(1, imageData2, alphaData2);
0777 
0778                 % Create image buffer
0779                 imBuffer = BufferedImage(wd, ht, BufferedImage.TYPE_INT_RGB);
0780                 imBuffer.setRGB(0, 0, wd, ht, typecast(imageData2(:), 'int32'), 0, wd);
0781 
0782                 % Create ImageSelection object from the image buffer
0783                 imSelection = ImageSelection(imBuffer);
0784 
0785                 % Set clipboard content to the image
0786                 cb.setContents(imSelection, []);
0787             catch
0788                 warning('export_fig -clipboard output failed: %s', lasterr); %#ok<LERR>
0789             end
0790         end
0791 
0792         % Don't output the data to console unless requested
0793         if ~nargout
0794             clear imageData alpha
0795         end
0796     catch err
0797         % Display possible workarounds before the error message
0798         if displaySuggestedWorkarounds && ~strcmpi(err.message,'export_fig error')
0799             if ~hadError,  fprintf(2, 'export_fig error. ');  end
0800             fprintf(2, 'Please ensure:\n');
0801             fprintf(2, '  that you are using the <a href="https://github.com/altmany/export_fig/archive/master.zip">latest version</a> of export_fig\n');
0802             if ismac
0803                 fprintf(2, '  and that you have <a href="http://pages.uoregon.edu/koch">Ghostscript</a> installed\n');
0804             else
0805                 fprintf(2, '  and that you have <a href="http://www.ghostscript.com">Ghostscript</a> installed\n');
0806             end
0807             try
0808                 if options.eps
0809                     fprintf(2, '  and that you have <a href="http://www.foolabs.com/xpdf">pdftops</a> installed\n');
0810                 end
0811             catch
0812                 % ignore - probably an error in parse_args
0813             end
0814             fprintf(2, '  and that you do not have <a href="matlab:which export_fig -all">multiple versions</a> of export_fig installed by mistake\n');
0815             fprintf(2, '  and that you did not made a mistake in the <a href="matlab:help export_fig">expected input arguments</a>\n');
0816             try
0817                 % Alert per issue #149
0818                 if ~strncmpi(get(0,'Units'),'pixel',5)
0819                     fprintf(2, '  or try to set groot''s Units property back to its default value of ''pixels'' (<a href="matlab:web(''https://github.com/altmany/export_fig/issues/149'',''-browser'');">details</a>)\n');
0820                 end
0821             catch
0822                 % ignore - maybe an old MAtlab release
0823             end
0824             fprintf(2, '\nIf the problem persists, then please <a href="https://github.com/altmany/export_fig/issues">report a new issue</a>.\n\n');
0825         end
0826         rethrow(err)
0827     end
0828 end
0829 
0830 function options = default_options()
0831     % Default options used by export_fig
0832     options = struct(...
0833         'name',         'export_fig_out', ...
0834         'crop',         true, ...
0835         'crop_amounts', nan(1,4), ...  % auto-crop all 4 image sides
0836         'transparent',  false, ...
0837         'renderer',     0, ...         % 0: default, 1: OpenGL, 2: ZBuffer, 3: Painters
0838         'pdf',          false, ...
0839         'eps',          false, ...
0840         'png',          false, ...
0841         'tif',          false, ...
0842         'jpg',          false, ...
0843         'bmp',          false, ...
0844         'clipboard',    false, ...
0845         'colourspace',  0, ...         % 0: RGB/gray, 1: CMYK, 2: gray
0846         'append',       false, ...
0847         'im',           false, ...
0848         'alpha',        false, ...
0849         'aa_factor',    0, ...
0850         'bb_padding',   0, ...
0851         'magnify',      [], ...
0852         'resolution',   [], ...
0853         'bookmark',     false, ...
0854         'closeFig',     false, ...
0855         'quality',      [], ...
0856         'update',       false, ...
0857         'fontswap',     true, ...
0858         'gs_options',   {{}});
0859 end
0860 
0861 function [fig, options] = parse_args(nout, fig, varargin)
0862     % Parse the input arguments
0863 
0864     % Set the defaults
0865     native = false; % Set resolution to native of an image
0866     options = default_options();
0867     options.im =    (nout == 1);  % user requested imageData output
0868     options.alpha = (nout == 2);  % user requested alpha output
0869 
0870     % Go through the other arguments
0871     skipNext = false;
0872     for a = 1:nargin-2
0873         if skipNext
0874             skipNext = false;
0875             continue;
0876         end
0877         if all(ishandle(varargin{a}))
0878             fig = varargin{a};
0879         elseif ischar(varargin{a}) && ~isempty(varargin{a})
0880             if varargin{a}(1) == '-'
0881                 switch lower(varargin{a}(2:end))
0882                     case 'nocrop'
0883                         options.crop = false;
0884                         options.crop_amounts = [0,0,0,0];
0885                     case {'trans', 'transparent'}
0886                         options.transparent = true;
0887                     case 'opengl'
0888                         options.renderer = 1;
0889                     case 'zbuffer'
0890                         options.renderer = 2;
0891                     case 'painters'
0892                         options.renderer = 3;
0893                     case 'pdf'
0894                         options.pdf = true;
0895                     case 'eps'
0896                         options.eps = true;
0897                     case 'png'
0898                         options.png = true;
0899                     case {'tif', 'tiff'}
0900                         options.tif = true;
0901                     case {'jpg', 'jpeg'}
0902                         options.jpg = true;
0903                     case 'bmp'
0904                         options.bmp = true;
0905                     case 'rgb'
0906                         options.colourspace = 0;
0907                     case 'cmyk'
0908                         options.colourspace = 1;
0909                     case {'gray', 'grey'}
0910                         options.colourspace = 2;
0911                     case {'a1', 'a2', 'a3', 'a4'}
0912                         options.aa_factor = str2double(varargin{a}(3));
0913                     case 'append'
0914                         options.append = true;
0915                     case 'bookmark'
0916                         options.bookmark = true;
0917                     case 'native'
0918                         native = true;
0919                     case 'clipboard'
0920                         options.clipboard = true;
0921                         options.im = true;
0922                         options.alpha = true;
0923                     case 'svg'
0924                         msg = ['SVG output is not supported by export_fig. Use one of the following alternatives:\n' ...
0925                                '  1. saveas(gcf,''filename.svg'')\n' ...
0926                                '  2. plot2svg utility: http://github.com/jschwizer99/plot2svg\n' ...
0927                                '  3. export_fig to EPS/PDF, then convert to SVG using generic (non-Matlab) tools\n'];
0928                         error(sprintf(msg)); %#ok<SPERR>
0929                     case 'update'
0930                         % Download the latest version of export_fig into the export_fig folder
0931                         try
0932                             zipFileName = 'https://github.com/altmany/export_fig/archive/master.zip';
0933                             folderName = fileparts(which(mfilename('fullpath')));
0934                             targetFileName = fullfile(folderName, datestr(now,'yyyy-mm-dd.zip'));
0935                             urlwrite(zipFileName,targetFileName);
0936                         catch
0937                             error('Could not download %s into %s\n',zipFileName,targetFileName);
0938                         end
0939 
0940                         % Unzip the downloaded zip file in the export_fig folder
0941                         try
0942                             unzip(targetFileName,folderName);
0943                         catch
0944                             error('Could not unzip %s\n',targetFileName);
0945                         end
0946                     case 'nofontswap'
0947                         options.fontswap = false;
0948                     otherwise
0949                         try
0950                             wasError = false;
0951                             if strcmpi(varargin{a}(1:2),'-d')
0952                                 varargin{a}(2) = 'd';  % ensure lowercase 'd'
0953                                 options.gs_options{end+1} = varargin{a};
0954                             elseif strcmpi(varargin{a}(1:2),'-c')
0955                                 if numel(varargin{a})==2
0956                                     skipNext = true;
0957                                     vals = str2num(varargin{a+1}); %#ok<ST2NM>
0958                                 else
0959                                     vals = str2num(varargin{a}(3:end)); %#ok<ST2NM>
0960                                 end
0961                                 if numel(vals)~=4
0962                                     wasError = true;
0963                                     error('option -c cannot be parsed: must be a 4-element numeric vector');
0964                                 end
0965                                 options.crop_amounts = vals;
0966                                 options.crop = true;
0967                             else  % scalar parameter value
0968                                 val = str2double(regexp(varargin{a}, '(?<=-(m|M|r|R|q|Q|p|P))-?\d*.?\d+', 'match'));
0969                                 if isempty(val) || isnan(val)
0970                                     % Issue #51: improved processing of input args (accept space between param name & value)
0971                                     val = str2double(varargin{a+1});
0972                                     if isscalar(val) && ~isnan(val)
0973                                         skipNext = true;
0974                                     end
0975                                 end
0976                                 if ~isscalar(val) || isnan(val)
0977                                     wasError = true;
0978                                     error('option %s is not recognised or cannot be parsed', varargin{a});
0979                                 end
0980                                 switch lower(varargin{a}(2))
0981                                     case 'm'
0982                                         % Magnification may never be negative
0983                                         if val <= 0
0984                                             wasError = true;
0985                                             error('Bad magnification value: %g (must be positive)', val);
0986                                         end
0987                                         options.magnify = val;
0988                                     case 'r'
0989                                         options.resolution = val;
0990                                     case 'q'
0991                                         options.quality = max(val, 0);
0992                                     case 'p'
0993                                         options.bb_padding = val;
0994                                 end
0995                             end
0996                         catch err
0997                             % We might have reached here by raising an intentional error
0998                             if wasError  % intentional raise
0999                                 rethrow(err)
1000                             else  % unintentional
1001                                 error(['Unrecognized export_fig input option: ''' varargin{a} '''']);
1002                             end
1003                         end
1004                 end
1005             else
1006                 [p, options.name, ext] = fileparts(varargin{a});
1007                 if ~isempty(p)
1008                     options.name = [p filesep options.name];
1009                 end
1010                 switch lower(ext)
1011                     case {'.tif', '.tiff'}
1012                         options.tif = true;
1013                     case {'.jpg', '.jpeg'}
1014                         options.jpg = true;
1015                     case '.png'
1016                         options.png = true;
1017                     case '.bmp'
1018                         options.bmp = true;
1019                     case '.eps'
1020                         options.eps = true;
1021                     case '.pdf'
1022                         options.pdf = true;
1023                     case '.fig'
1024                         % If no open figure, then load the specified .fig file and continue
1025                         if isempty(fig)
1026                             fig = openfig(varargin{a},'invisible');
1027                             varargin{a} = fig;
1028                             options.closeFig = true;
1029                         else
1030                             % save the current figure as the specified .fig file and exit
1031                             saveas(fig(1),varargin{a});
1032                             fig = -1;
1033                             return
1034                         end
1035                     case '.svg'
1036                         msg = ['SVG output is not supported by export_fig. Use one of the following alternatives:\n' ...
1037                                '  1. saveas(gcf,''filename.svg'')\n' ...
1038                                '  2. plot2svg utility: http://github.com/jschwizer99/plot2svg\n' ...
1039                                '  3. export_fig to EPS/PDF, then convert to SVG using generic (non-Matlab) tools\n'];
1040                         error(sprintf(msg)); %#ok<SPERR>
1041                     otherwise
1042                         options.name = varargin{a};
1043                 end
1044             end
1045         end
1046     end
1047 
1048     % Quick bail-out if no figure found
1049     if isempty(fig),  return;  end
1050 
1051     % Do border padding with repsect to a cropped image
1052     if options.bb_padding
1053         options.crop = true;
1054     end
1055 
1056     % Set default anti-aliasing now we know the renderer
1057     if options.aa_factor == 0
1058         try isAA = strcmp(get(ancestor(fig, 'figure'), 'GraphicsSmoothing'), 'on'); catch, isAA = false; end
1059         options.aa_factor = 1 + 2 * (~(using_hg2(fig) && isAA) | (options.renderer == 3));
1060     end
1061 
1062     % Convert user dir '~' to full path
1063     if numel(options.name) > 2 && options.name(1) == '~' && (options.name(2) == '/' || options.name(2) == '\')
1064         options.name = fullfile(char(java.lang.System.getProperty('user.home')), options.name(2:end));
1065     end
1066 
1067     % Compute the magnification and resolution
1068     if isempty(options.magnify)
1069         if isempty(options.resolution)
1070             options.magnify = 1;
1071             options.resolution = 864;
1072         else
1073             options.magnify = options.resolution ./ get(0, 'ScreenPixelsPerInch');
1074         end
1075     elseif isempty(options.resolution)
1076         options.resolution = 864;
1077     end
1078 
1079     % Set the default format
1080     if ~isvector(options) && ~isbitmap(options)
1081         options.png = true;
1082     end
1083 
1084     % Check whether transparent background is wanted (old way)
1085     if isequal(get(ancestor(fig(1), 'figure'), 'Color'), 'none')
1086         options.transparent = true;
1087     end
1088 
1089     % If requested, set the resolution to the native vertical resolution of the
1090     % first suitable image found
1091     if native && isbitmap(options)
1092         % Find a suitable image
1093         list = findall(fig, 'Type','image', 'Tag','export_fig_native');
1094         if isempty(list)
1095             list = findall(fig, 'Type','image', 'Visible','on');
1096         end
1097         for hIm = list(:)'
1098             % Check height is >= 2
1099             height = size(get(hIm, 'CData'), 1);
1100             if height < 2
1101                 continue
1102             end
1103             % Account for the image filling only part of the axes, or vice versa
1104             yl = get(hIm, 'YData');
1105             if isscalar(yl)
1106                 yl = [yl(1)-0.5 yl(1)+height+0.5];
1107             else
1108                 yl = [min(yl), max(yl)];  % fix issue #151 (case of yl containing more than 2 elements)
1109                 if ~diff(yl)
1110                     continue
1111                 end
1112                 yl = yl + [-0.5 0.5] * (diff(yl) / (height - 1));
1113             end
1114             hAx = get(hIm, 'Parent');
1115             yl2 = get(hAx, 'YLim');
1116             % Find the pixel height of the axes
1117             oldUnits = get(hAx, 'Units');
1118             set(hAx, 'Units', 'pixels');
1119             pos = get(hAx, 'Position');
1120             set(hAx, 'Units', oldUnits);
1121             if ~pos(4)
1122                 continue
1123             end
1124             % Found a suitable image
1125             % Account for stretch-to-fill being disabled
1126             pbar = get(hAx, 'PlotBoxAspectRatio');
1127             pos = min(pos(4), pbar(2)*pos(3)/pbar(1));
1128             % Set the magnification to give native resolution
1129             options.magnify = abs((height * diff(yl2)) / (pos * diff(yl)));  % magnification must never be negative: issue #103
1130             break
1131         end
1132     end
1133 end
1134 
1135 function A = downsize(A, factor)
1136     % Downsample an image
1137     if factor == 1
1138         % Nothing to do
1139         return
1140     end
1141     try
1142         % Faster, but requires image processing toolbox
1143         A = imresize(A, 1/factor, 'bilinear');
1144     catch
1145         % No image processing toolbox - resize manually
1146         % Lowpass filter - use Gaussian as is separable, so faster
1147         % Compute the 1d Gaussian filter
1148         filt = (-factor-1:factor+1) / (factor * 0.6);
1149         filt = exp(-filt .* filt);
1150         % Normalize the filter
1151         filt = single(filt / sum(filt));
1152         % Filter the image
1153         padding = floor(numel(filt) / 2);
1154         for a = 1:size(A, 3)
1155             A(:,:,a) = conv2(filt, filt', single(A([ones(1, padding) 1:end repmat(end, 1, padding)],[ones(1, padding) 1:end repmat(end, 1, padding)],a)), 'valid');
1156         end
1157         % Subsample
1158         A = A(1+floor(mod(end-1, factor)/2):factor:end,1+floor(mod(end-1, factor)/2):factor:end,:);
1159     end
1160 end
1161 
1162 function A = rgb2grey(A)
1163     A = cast(reshape(reshape(single(A), [], 3) * single([0.299; 0.587; 0.114]), size(A, 1), size(A, 2)), class(A)); %#ok<ZEROLIKE>
1164 end
1165 
1166 function A = check_greyscale(A)
1167     % Check if the image is greyscale
1168     if size(A, 3) == 3 && ...
1169             all(reshape(A(:,:,1) == A(:,:,2), [], 1)) && ...
1170             all(reshape(A(:,:,2) == A(:,:,3), [], 1))
1171         A = A(:,:,1); % Save only one channel for 8-bit output
1172     end
1173 end
1174 
1175 function eps_remove_background(fname, count)
1176     % Remove the background of an eps file
1177     % Open the file
1178     fh = fopen(fname, 'r+');
1179     if fh == -1
1180         error('Not able to open file %s.', fname);
1181     end
1182     % Read the file line by line
1183     while count
1184         % Get the next line
1185         l = fgets(fh);
1186         if isequal(l, -1)
1187             break; % Quit, no rectangle found
1188         end
1189         % Check if the line contains the background rectangle
1190         if isequal(regexp(l, ' *0 +0 +\d+ +\d+ +r[fe] *[\n\r]+', 'start'), 1)
1191             % Set the line to whitespace and quit
1192             l(1:regexp(l, '[\n\r]', 'start', 'once')-1) = ' ';
1193             fseek(fh, -numel(l), 0);
1194             fprintf(fh, l);
1195             % Reduce the count
1196             count = count - 1;
1197         end
1198     end
1199     % Close the file
1200     fclose(fh);
1201 end
1202 
1203 function b = isvector(options)
1204     b = options.pdf || options.eps;
1205 end
1206 
1207 function b = isbitmap(options)
1208     b = options.png || options.tif || options.jpg || options.bmp || options.im || options.alpha;
1209 end
1210 
1211 % Helper function
1212 function A = make_cell(A)
1213     if ~iscell(A)
1214         A = {A};
1215     end
1216 end
1217 
1218 function add_bookmark(fname, bookmark_text)
1219     % Adds a bookmark to the temporary EPS file after %%EndPageSetup
1220     % Read in the file
1221     fh = fopen(fname, 'r');
1222     if fh == -1
1223         error('File %s not found.', fname);
1224     end
1225     try
1226         fstrm = fread(fh, '*char')';
1227     catch ex
1228         fclose(fh);
1229         rethrow(ex);
1230     end
1231     fclose(fh);
1232 
1233     % Include standard pdfmark prolog to maximize compatibility
1234     fstrm = strrep(fstrm, '%%BeginProlog', sprintf('%%%%BeginProlog\n/pdfmark where {pop} {userdict /pdfmark /cleartomark load put} ifelse'));
1235     % Add page bookmark
1236     fstrm = strrep(fstrm, '%%EndPageSetup', sprintf('%%%%EndPageSetup\n[ /Title (%s) /OUT pdfmark',bookmark_text));
1237 
1238     % Write out the updated file
1239     fh = fopen(fname, 'w');
1240     if fh == -1
1241         error('Unable to open %s for writing.', fname);
1242     end
1243     try
1244         fwrite(fh, fstrm, 'char*1');
1245     catch ex
1246         fclose(fh);
1247         rethrow(ex);
1248     end
1249     fclose(fh);
1250 end
1251 
1252 function set_tick_mode(Hlims, ax)
1253     % Set the tick mode of linear axes to manual
1254     % Leave log axes alone as these are tricky
1255     M = get(Hlims, [ax 'Scale']);
1256     if ~iscell(M)
1257         M = {M};
1258     end
1259     M = cellfun(@(c) strcmp(c, 'linear'), M);
1260     set(Hlims(M), [ax 'TickMode'], 'manual');
1261     %set(Hlims(M), [ax 'TickLabelMode'], 'manual');  % this hides exponent label in HG2!
1262 end
1263 
1264 function change_rgb_to_cmyk(fname)  % convert RGB => CMYK within an EPS file
1265     % Do post-processing on the eps file
1266     try
1267         % Read the EPS file into memory
1268         fstrm = read_write_entire_textfile(fname);
1269 
1270         % Replace all gray-scale colors
1271         fstrm = regexprep(fstrm, '\n([\d.]+) +GC\n', '\n0 0 0 ${num2str(1-str2num($1))} CC\n');
1272         
1273         % Replace all RGB colors
1274         fstrm = regexprep(fstrm, '\n[0.]+ +[0.]+ +[0.]+ +RC\n', '\n0 0 0 1 CC\n');  % pure black
1275         fstrm = regexprep(fstrm, '\n([\d.]+) +([\d.]+) +([\d.]+) +RC\n', '\n${sprintf(''%.4g '',[1-[str2num($1),str2num($2),str2num($3)]/max([str2num($1),str2num($2),str2num($3)]),1-max([str2num($1),str2num($2),str2num($3)])])} CC\n');
1276 
1277         % Overwrite the file with the modified contents
1278         read_write_entire_textfile(fname, fstrm);
1279     catch
1280         % never mind - leave as is...
1281     end
1282 end

Generated on Tue 23-May-2017 20:00:55 by m2html © 2005