4.89474

4.9 | 20 ratings Rate this file 322 downloads (last 30 days) File Size: 13.03 KB File ID: #10889

savefig

by Peder Axensten

 

27 Apr 2006 (Updated 08 Nov 2008)

Code covered by BSD License  

Save *cropped* figures to pdf, eps, png, jpeg, and tiff through Ghostscript.

Download Now | Watch this File

File Information
Description

Usage: savefig(filename, fighdl, options)

Saves a pdf, eps, png, jpeg, and/or tiff of the contents of the fighandle's (or current) figure.
It saves an eps of the figure and the uses Ghostscript to convert to the other formats.
The result is a cropped, clean picture. There are options for using rgb or cmyk colours,
or grayscale. You can also choose the resolution.

The advantage of savefig is that there is very little empty space around the figure in the
resulting files, you can export to more than one format at once, and Ghostscript generates
trouble-free files.

If you find any errors, please let me know! (peder at axensten dot se)

filename: File name without suffix.

fighdl: (default: gcf) Integer handle to figure.

options: (default: '-r300', '-lossless', '-rgb') You can define your own
         defaults in a global variable savefig_defaults, if you want to, i.e.
         savefig_defaults= {'-r200','-gray'};.
'eps': Output in Encapsulated Post Script (no preview yet).
'pdf': Output in (Adobe) Portable Document Format.
'png': Output in Portable Network Graphics.
'jpeg': Output in Joint Photographic Experts Group format.
'tiff': Output in Tagged Image File Format (no compression: huge files!).
'-rgb': Output in rgb colours.
'-cmyk': Output in cmyk colours (not yet 'png' or 'jpeg' -- '-rgb' is used).
'-gray': Output in grayscale (not yet 'eps' -- '-rgb' is used).
'-fonts': Include fonts in eps or pdf. Includes only the subset needed.
'-lossless': Use lossless compression, works on most formats. same as '-c0', below.
'-c<float>': Set compression for non-indexed bitmaps in PDFs -
              0: lossless; 0.1: high quality; 0.5: medium; 1: high compression.
'-r<integer>': Set resolution.
'-crop': Removes points and line segments outside the viewing area -- permanently.
         Only use this on figures where many points and/or line segments are outside
         the area zoomed in to. This option will result in smaller vector files (has no
         effect on pixel files).
'-dbg': Displays gs command line(s).

EXAMPLE:
savefig('nicefig', 'pdf', 'jpeg', '-cmyk', '-c0.1', '-r250');
Saves the current figure to nicefig.pdf and nicefig.png, both in cmyk and at 250 dpi,
         with high quality lossy compression.

REQUIREMENT: Ghostscript. Version 8.57 works, probably older versions too, but '-dEPSCrop'
         must be supported. I think version 7.32 or newer is ok.

HISTORY:
Version 1.0, 2006-04-20.
Version 1.1, 2006-04-27:
- No 'epstopdf' stuff anymore! Using '-dEPSCrop' option in gs instead!
Version 1.2, 2006-05-02:
- Added a '-dbg' option (see options, above).
- Now looks for a global variable 'savefig_defaults' (see options, above).
- More detailed Ghostscript options (user will not really notice).
- Warns when there is no device for a file-type/color-model combination.
Version 1.3, 2006-06-06:
- Added a check to see if there actually is a figure handle.
- Now works in Matlab 6.5.1 (R13SP1) (maybe in 6.5 too).
- Now compatible with Ghostscript 8.54, released 2006-06-01.
Version 1.4, 2006-07-20:
- Added an option '-soft' that enables anti-aliasing on pixel graphics (on by default).
- Added an option '-hard' that don't do anti-aliasing on pixel graphics.
Version 1.5, 2006-07-27:
- Fixed a bug when calling with a figure handle argument.
Version 1.6, 2006-07-28:
- Added a crop option, see above.
Version 1.7, 2007-03-31:
- Fixed bug: calling print with invalid renderer value '-none'.
- Removed GhostScript argument '-dUseCIEColor' as it sometimes discoloured things.
Version 1.8, 2008-01-03:
- Added MacIntel: 'MACI'.
- Added 64bit PC (I think, can't test it myself).
- Added option '-nointerpolate' (use it to prevent blurring of pixelated).
- Removed '-hard' and '-soft'. Use '-nointerpolate' for '-hard', default for '-soft'.
- Fixed the gs 8.57 warning on UseCIEColor (it's now set).
- Added '-gray' for pdf, but gs 8.56 or newer is needed.
- Added '-gray' and '-cmyk' for eps, but you a fairly recent gs might be needed.
Version 1.9, 2008-07-27:
- Added lossless compression, see option '-lossless', above. Works on most formats.
- Added lossy compression, see options '-c<float>...', above. Works on 'pdf'.
  Thanks to Olly Woodford for idea and implementation!
- Removed option '-nointerpolate' -- now savefig never interpolates.
- Fixed a few small bugs and removed some mlint comments.
Version 2.0, 2008-11-07:
- Added the possibility to include fonts into eps or pdf.

TO DO: (Need Ghostscript support for these, so don't expect anything soon...)
- svg output.
- '-cmyk' for 'jpeg' and 'png'.
- Preview in 'eps'.
- Embedded vector fonts, not bitmap, in 'eps'.

Copyright (C) Peder Axensten (peder at axensten dot se), 2006.

Acknowledgements

The author wishes to acknowledge the following in the creation of this submission:
eps2pdf
This submission has inspired the following:
Quick_Figure, print_eps, print_pdf, export_fig

MATLAB release MATLAB 7.4 (R2007a)
Other requirements Ghostscript. Version 8.57 or later works. The option '-dEPSCrop' must be supported, so I guess version 7.32 or later is ok.
Zip File Content  
Other Files license.txt,
savefig.m
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (22)
28 Apr 2006 M. Tetouan

Excellent! The default cropping option is very useful for LaTeX and PowerPoint users.

23 Jun 2006 Ted Rosenbaum

I second the previous comment, this produces very clean and properly trimmed eps files for inclusion in latex.

03 Aug 2006 K H

Very easy to use - works well

03 Dec 2006 Raphael P

very easy to use. great interface, and great results.

16 Jan 2007 Simone Hochgreb

Brilliant. I wish I hadn't spent so much time trying to fix matlab bugs. Thank you.

31 Jan 2007 Felix Hebeler

Great, I wish I had looked earlier... finally 4 figures do have the same extent, colorbarbwidth and readable font. If only grayscale would work for eps and pdfs...

22 Mar 2007 Dan Hemberger

This is a great file. It is exactly what I was looking for. I wanted to use PDF files from Matlab with pdflatex, but I was encountering bounding box issues.

A note, however: I had to include the path to Ghostscript .exe in the script.

21 Jun 2007 Felix Hebeler

Forgot to rate last time. Nice script. Would be good if one could specify fontsize. I use a workaround atm, opening the export figure dialog and selecting (and applying) font size there, then use savefig

26 Oct 2007 Martin Hemberg

Great. Does what it says without any problems

29 Oct 2007 Tom D

Great stuff! This functionality really ought to be included in MATLAB. By default, savefig interpolates images, which can be undesirable when plotting discrete data like 2-D histograms. A quick fix is to delete -dDOINTERPOLATE from line 98. I have sent Peter an email requesting him to make this behavior be controlled by a switch

16 Feb 2008 Derek O'Connor

This is top class. Tight bounding box and small file size (eps).

QUESTION : Why doesn't Matlab get it right?

16 Feb 2008 Derek O'Connor

I forgot to add this in my previous post.

For Windows Vista 64 : after line 93, add a copy of line 93 with 'PCWIN' changed to 'PCWIN64'

16 May 2008 Ben S

This program does exactly what Matlab should do with graphic exports.

Why does Matlab decide to completely chew up graphs when exported directly to .pdf? And if you export through .eps and use epstopdf you get an idiotic bounding box. Matlab has the ability to set up page size and attributes in the page setup menu, but not in the export setup menu. And they don't seem to be in any way related with regards to pdf's. Pretty poor.

Anyway, this program fixes all that. But, I had a problem whereby it didn't recognise my gs file location so I had to copy gswin32c.exe into my working Matlab folder. Fine now.

30 May 2008 Oliver Woodford

Very nice, but could do with having more options for compression when converting to pdf (and also jpeg, I guess) - e.g. lossless, high, medium, low - as the default setting is pretty low quality/high compression. For my purposes I needed higher quality output though.

I figured out how to do this myself, so if you want the gs incantations for this I can send them to you.

05 Jun 2008 Claudio Piciarelli

simply great, it's exactly what I was searching for and it works fine :)

02 Jul 2008 DS .

Functional and very handy code. There appear to be some issues with regard to proper cropping of figures, however. Possibly a ghostscript issue, but when saving as a .tif image the procedure produces uneven padded borders. Saving and 1024x512 pixel .tif at 150dpi gives me border padding of 1,7,7, and 2 pixels, respectively (using GPL Ghostscript 8.62).

09 Jul 2008 Oliver Woodford

I should clarify that my previous comment was specific to saving images overlaid with vector graphics to pdf, which is probably quite a niche requirement.

16 Sep 2008 Michal Aftanas

Simply great, exactly what I was looking for a long time. Good job

29 Sep 2008 A Muir

Great little script. The real question is why the built in Matlab graphic export is so incredibly bad in the first place. thankyou for correcting their mistakes

24 Oct 2008 Oliver Woodford

With the ability to change compression quality for bitmaps in pdfs, this is now perfect. Highly recommended alternative to Matlab's PRINT.

09 Nov 2008 Salman

just great

17 Apr 2009 Dylan

I'm using this script to export figures to pdf. The figures have an image background, with vector elements (dots) overlaid. Sometimes the vector elements are kept as vectors, sometimes they are merged with the image... and I can't work out why, or how to force vector elements to be retained. It may have to do with the complexity of the figure. Any suggestions would be appreciated. Otherwise, great script!

Please login to add a comment or rating.
Updates
02 May 2006

- Added a '-dbg' option.
- Now looks for a global variable 'savefig_defaults'.
- More detailed Ghostscript options.
- Warns when there is no device for a file-type/color-model combination.

16 May 2006

N/A

06 Jun 2006

- Added a check to see if there actually is a figure handle.
- Now works in Matlab 6.5.1 (R13SP1) (maybe in 6.5 too).
- Now compatible with Ghostscript 8.54, released 2006-06-01.

24 Jul 2006

- Added an option '-soft' that enables anti-aliasing on pixel graphics (on by default).
- Added an option '-hard' that don't do anti-aliasing on pixel graphics.

28 Jul 2006

Fixed a bug when calling with a figure handle argument.

31 Jul 2006

Added a crop option.

01 Apr 2007

- Fixed bug: calling print with invalid renderer value '-none'.
- Removed GhostScript argument '-dUseCIEColor' as it sometimes discoloured things.

28 Jul 2008

- Added MacIntel.
- Added 64bit PC.
- Added option '-nointerpolate'.
 - Removed '-hard' and '-soft'.
- Fixed the gs 8.57 warning on UseCIEColor.
- Added '-gray' for pdf.
- Added '-gray' and '-cmyk' for eps.

04 Aug 2008

- Added lossless compression.
- Added lossy compression.
- Removed option '-nointerpolate'.
- Fixed a few small bugs and removed some mlint comments.

08 Nov 2008

- Added the possibility to include fonts into a pdf.

Tag Activity for this File
Tag Applied By Date/Time
graphics import Peder Axensten 22 Oct 2008 08:23:27
graphics export Peder Axensten 22 Oct 2008 08:23:27
eps Peder Axensten 22 Oct 2008 08:23:27
pdf Peder Axensten 22 Oct 2008 08:23:27
jpg Peder Axensten 22 Oct 2008 08:23:27
jpeg Peder Axensten 22 Oct 2008 08:23:27
png Peder Axensten 22 Oct 2008 08:23:27
convert Peder Axensten 22 Oct 2008 08:23:27
eps2pdf Peder Axensten 22 Oct 2008 08:23:27
epstopdf Peder Axensten 22 Oct 2008 08:23:27
tiff Peder Axensten 22 Oct 2008 08:23:27
graphics import Cristina McIntire 10 Nov 2008 10:34:26
graphics export Cristina McIntire 10 Nov 2008 10:34:32
convert Cristina McIntire 10 Nov 2008 10:34:36
pdf Adam 20 May 2009 09:48:16
 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com