Exporting Figures in Matlab

Exporting figures from Matlab to eps (or many other formats) is relatively straightforward using the print -d<format> command. A problem rises, however, when you try to use the pdf format as the resulting pdf has incorrect margins and does not work well when you include it in a latex document. Also, it sometimes nice to have multiple types of figures created using a single Matlab command.

To export figures for publication, the third-party export_fig works extremely well. It requires Ghostscript and xpdf (to install on ubuntu: sudo aptitude install xpdf ghostscript).

For example,

export_fig nicefig -pdf -eps     % saves current fig as pdf and eps

saves the current figure as both nicefig.pdf and nicefig.eps. For archiving and/or publications, it is often convenient to automatically name figures and save them in four formats: eps, pdf, png, and fig (matlab). The script savefig.m does this by wrapping export_fig.

A second option is to use savefig. It has the advantage of less dependencies, however the output is not as nice (e.g., pdfs have untrimmed margins).

savefig('nicefig', 'pdf');     % saves current fig as pdf

If you want to get eps and convert it manually,

savefig('nicefig', 'eps');     % saves current fig as eps

After generating the eps, use ps2pdf to crop (maintaining current image resolution).

ps2pdf -dEPSCrop -dAutoRotatePages=/None -dAutoFilterColorImages=false -dColorImageFilter=/FlateEncode -dUseFlateCompression=true nicefig.eps

If available,

epstopdf nicefig.eps

does this automatically (to install on ubuntu: sudo aptitude install texlive-extra-utils).