5.0

5.0 | 10 ratings Rate this file 804 downloads (last 30 days) File Size: 15.07 KB File ID: #23629

export_fig

by Oliver Woodford

 

11 Apr 2009 (Updated 09 Oct 2009)

Code covered by BSD License  

Exports figures nicely to a number of vector & bitmap formats.

Download Now | Watch this File

File Information
Description

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
   - Embedded fonts (vector formats)
   - Improved line and grid line styles (vector formats)
   - Anti-aliased graphics (bitmap formats)
   - Transparent background supported (pdf, eps, png)
   - Semi-transparent patch objects supported (png only)
   - CMYK output optional (pdf, eps, tiff)
   - Append to file (pdf only)
   - Vector formats: pdf, eps
   - Bitmap formats: png, tif, 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), set the figure (and axes) 'Color' property to 'none'; pdf, eps and png are the only file formats to support a transparent background, whilst the png format alone supports transparency of patch objects.

When exporting to vector format (pdf & eps), 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

When reporting bugs, please use the 'Contact Author' link on my Author page, rather than pasting the error into the comments - I will respond quicker, especially if I can email you back.

Acknowledgements

The author wishes to acknowledge the following in the creation of this submission:
savefig, Fix dashed and dotted lines in EPS export, FIXPSLINESTYLE, Myaa - My Anti-Alias for Matlab, print_pdf, print2im, print_eps, fix_lines
This submission has inspired the following:
Matlab Icon for Linux (with Transparency)

MATLAB release MATLAB 7.5 (R2007b)
Other requirements Ghostscript, Xpdf
Zip File Content  
Other Files eps2pdf.m,
export_fig.m,
fix_lines.m,
license.txt,
pdf2eps.m,
print2array.m,
print2eps.m,
private/ghostscript.m,
private/pdftops.m
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (22)
15 Apr 2009 danny

Hello! How can I use your program to export a MATLAB graph to .eps or .pdf format with size changes ?

Thank you,
dan

17 Apr 2009 Matt C

This suite of exportation options is really great! I used to cook up these functions on my own, but with the frontend you created this is now a lot simpler.

When using it I found that the way you are currently implementing ghostscript will destroy the destination file if it already exists. While this may be the intended implementation, let me offer an alternative that could possibly be as useful to others.

I have edited the 'eps2pdf.m' file to allow for appending additional figures to an already existing .pdf file as follows:
<code>

function eps2pdf(source, dest)
% Construct the options string for ghostscript - create new or append
% additional sheets if the destination exists
if (exist(dest, 'file') == 2)
    tmp_nam = tempname;
    copyfile(dest, tmp_nam);
    options = ['-q -dNOPAUSE -dBATCH -dEPSCrop -sDEVICE=pdfwrite -dPDFSETTINGS=/prepress -sOutputFile="' dest '" "' tmp_nam '" -f "' source '"'];
else
    options = ['-q -dNOPAUSE -dBATCH -dEPSCrop -sDEVICE=pdfwrite -dPDFSETTINGS=/prepress -sOutputFile="' dest '" -f "' source '"'];
end
    
% Convert to pdf using ghostscript
ghostscript(options);
return

</code>

I hope some of you can find this modification useful!

18 Apr 2009 Mohammed Islam

Hi Oliver,
Thanks soo much for this script. It's of great use for me. So far I haven't had any trouble using it. Just one question, is it possible to use it to control the size of the output file?

Also, thanks to Matt for the addition, Its nice to not replace an existing file and keep adding up additional figues at the bottom. Thats what I wanted to do.

Great job guys. keep it up.

Shameem

19 Apr 2009 Oliver Woodford

Dan: Currently the only way to change the exported figure size is to change the Matlab figure size before exporting. To get round this, set the paper size options manually within export_fig.

Matt C: Thanks for the great suggestion.

Shameem/Mohammed: I wanted to keep the interface simple, so unfortunately there is no formal way to control file size. Jpeg file size can be changed by varying the quality setting within export_fig. For more options regarding quality/compression, see savefig (also on the FEX).

21 Apr 2009 Christian Dorion

Great piece of work, thanks.

A minor thing though... Under my installation, the private/ghostscript.m function had a minor bug that resulted in the whole function being whipped out and replaced by solely the 'gs' path. Just in case it happens to anyone else, here is a simple patch:

--- MY/private/ghostscript.m
+++ ONLINE/private/ghostscript.m
@@ -103,9 +103,8 @@
 fstrm = fread(fh, '*char')';
 fclose(fh);
 % Find the path
-first_sec = regexp(fstrm, '[\n\r]*function path = current_gs_path[\n\r]*path = ''', 'end', 'once');
-second_sec = first_sec + regexp(fstrm(first_sec+1:end), ''';[\n\r]*return', 'once');
-assert(~isempty(first_sec) && ~isempty(second_sec));
+first_sec = regexp(fstrm, '[\n\r]function path = current_gs_path[\n\r]path = ''', 'end', 'once');
+second_sec = first_sec + regexp(fstrm(first_sec+1:end), ''';[\n\r]return', 'once');
 % Save the file with the path replaced
 fh = fopen(fname, 'wt');
 fprintf(fh, '%s%s%s', fstrm(1:first_sec), path, fstrm(second_sec:end));

28 May 2009 Edgar Guevara Codina

Great job!
Finally I can get publication-quality graphs in a simple way.
Thanks Oliver for making it easier than ever before.

28 May 2009 wei feng

Great work! You sir saved me a LOT of time.

I've been using your print_eps function for a while and found this today. However, I have a problem with converting images to eps format that has been bugging me. For example, I have two images in a matlab figure. On both images I have used the "plot" function to plot some lines on the images. The plotted lines are gray in color. When I export them into eps format, the lines on the first image would remain gray. But the lines on the second image would change to black. The same thing happens when I have more than two images with plotted lines, in which case all the plotted lines except in the first image will turn black. I was wondering if you can shed some light on this?

Thanks a lot.

03 Jun 2009 Stefan

Great job!

But still one bug: Exporting does not work if axes is child of uipanel. Seems to be some problem when using cobyobj
Can you fix that?

29 Jun 2009 Daisy Fan

Nice!

How can I get a high resolution eps, e.g., >300dpi? Matlab's documentation says to use the -r option, e.g.,
  print -deps -r300 myfile
but it actually doesn't work. When I look at the produced eps in a software such as Adobe Photoshop the resolution is just 72dpi. Thanks for you input!

02 Jul 2009 Gabriel Akira Schreiber  
03 Jul 2009 Gabriel Akira Schreiber  
07 Jul 2009 Johannes Korsawe

Really nice.
I still have a problem using uipanels (guis):

 If i create a plot and a colorbar, the colorbar will not be printed in the result.

Is it possible to enable for a search for colorbars? I tried to adopt your code but failed in copying the correct stuctures.

To say it with Stefan: Can you fix/enable that?

08 Jul 2009 Sebastiaan  
14 Jul 2009 cartonn

how do you export the image with a greater value of dpi?
-r600 does not work

22 Jul 2009 Jason Mar

Great function! It saves me a lot of time, thanks!
Wish I could disable cropping more easily though.

31 Jul 2009 Annette

I'm trying to use this function with Matlab r2007b on a Mac (OS 10.5), and I get the error:

??? Error using ==> rdivide
Number of array dimensions must match for binary array op.

Error in ==> export_fig at 143
        A = uint8(B ./ A(:,:,[1 1 1]));

Any insight?

Cheers!

14 Sep 2009 m g

I am unable to use export_fig on my linux machine
i am using the command
export_fig f.fig -eps
and i get the following error:
'No Figure to Print"
and a Warning /tmp/tmp<long string>.eps not found

please help.
thanks,

18 Sep 2009 Erica Alston

Hi

What is the calling sequence for using this function? should the filename be in parentheses? are the output formats the same as used by matlap e.g. -depsc?

Many thanks!

22 Sep 2009 Oliver Woodford

m g: The figure you wish to export must be open. You cannot specify it by the filename. The filename is that of the output file, not the input file.

Erica: The filename should be a string. Just like any other MATLAB function, you can call export_fig using parentheses around the input arguments, or not, in which case the inputs are interpreted as strings; i.e. if one of the inputs is not a string then you must use parentheses. The outputs are not the same as those used by print. Type
   help export_fig
for a full description of usage.

27 Sep 2009 Francesco Montorsi  
20 Oct 2009 Michael Kitz

I wanted to use this tool to save a jpg with the borders cropped. Now I realized that the axes settings were changed during export.
I was using a figure with three subplots, two graphs and an image.
The plots were displaying 7000 data points in a customized scale using the whole axes area. Matlab's autoscale uses 8000 points and so did the export function.
Is there a way to avoid the autoscale?

26 Oct 2009 Oliver Woodford

Michael: MATLAB's print often does funny things to a plot, such as changing the number of tick marks, when you export it, and I don't know how to avoid it. Export_fig uses print, and therefore unfortunately suffers from the same problem.

Please login to add a comment or rating.
Updates
15 Apr 2009

Added ability to export rasterized figure straight to the workspace.

19 Apr 2009

Added ability to append to pdf files, as suggested by Matt C. Corrected some help text.

21 Apr 2009

Incorporated Christian Dorion's patch for updating ghostscript (and also pdftops) path. Fixed bug that didn't correctly detect if pdftops path was valid (due to a MATLAB/Windows bug).

24 Apr 2009

Added ability to compute transparency of patch objects when outputting to png and exporting to workspace.

28 Jun 2009

Fixed bug to allow exporting of axes that are children of uipanels. Thanks to Stefan for pointing out the bug.

21 Jul 2009

Export colorbars associated with subplots when in a uipanel. Thanks to Johannes Korsawe for pointing out the bug.

29 Jul 2009

Added options to disable cropping and set level of anti-aliasing. Added a fix to get round a rendering bug in print (thanks to Johannes Korsawe for pointing it out).

31 Jul 2009

Added option to use zbuffer renderer for bitmaps. This avoids some errors caused by bugs in the opengl renderer.
Fixed bug when exporting greyscale figures with an alphamatte (thanks to Annette for pointing it out).

09 Oct 2009

Added ability to export at any resolution (bitmap outputs only) and also in CMYK colorspace (tiff, pdf, eps only).

Tag Activity for this File
Tag Applied By Date/Time
data export Oliver Woodford 13 Apr 2009 10:30:18
export figure Oliver Woodford 13 Apr 2009 10:30:18
print Oliver Woodford 13 Apr 2009 10:30:18
anti alias Oliver Woodford 13 Apr 2009 10:30:18
embed Oliver Woodford 13 Apr 2009 10:30:18
fonts Oliver Woodford 13 Apr 2009 10:30:18
crop Oliver Woodford 13 Apr 2009 10:30:18
border Oliver Woodford 13 Apr 2009 10:30:18
figure Oliver Woodford 13 Apr 2009 10:30:18
save Oliver Woodford 13 Apr 2009 10:30:18
saveas Oliver Woodford 13 Apr 2009 10:30:18
transparency Oliver Woodford 13 Apr 2009 10:30:18
bitmap Oliver Woodford 13 Apr 2009 10:30:18
vector graphics Oliver Woodford 13 Apr 2009 10:30:18
pdf Oliver Woodford 13 Apr 2009 10:30:18
bmp Oliver Woodford 13 Apr 2009 10:30:18
png Oliver Woodford 13 Apr 2009 10:30:18
tiff Oliver Woodford 13 Apr 2009 10:30:18
jpeg Oliver Woodford 13 Apr 2009 10:30:18
tif Oliver Woodford 13 Apr 2009 10:30:18
jpg Oliver Woodford 13 Apr 2009 10:30:18
eps Oliver Woodford 13 Apr 2009 10:30:18
rasterize Oliver Woodford 15 Apr 2009 13:46:29
workspace Oliver Woodford 15 Apr 2009 13:46:29
append Oliver Woodford 20 Apr 2009 11:50:47
transparent Oliver Woodford 24 Apr 2009 07:29:19
jpeg Olav 27 May 2009 04:43:33
anti alias cartonn 13 Jul 2009 12:09:51
anti alias Jaka Katrasnik 28 Jul 2009 03:48:04
cmyk Oliver Woodford 10 Oct 2009 19:08:46
marker size Q Zhang 02 Nov 2009 10:26:20
 

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