About Matlab figure export in pdf/eps

Post date: Sep 22, 2010 8:51:6 AM

I export my Matlab figures in the pdf format and this produces a nice a4 or usletter size pdf document with my plot. 

In fact, Matlab creates a pdf of the size indicated in the 'papertype' or 'papersize' figure properties. 

Most of the time I don't care because my plots fit in a landscape or portrait a4/usletter page.

But when comes the time to write a clean article or a report and embed my figure in a LaTeX document, I want to get rid of all the white space around the plot and have a nice pdf file fitting the plot.

You probably noticed that exporting the figure in the eps format will produce an eps file fitting the plot without extra white spaces around it. 

With my Mac, opening the eps file (with Preview) creates a pdf file exactly as I want it (without white space) and then I just have to "Save as" the file and I'm done.

But there's a more automatic way to do it, without relying on the Mac OS Preview software to produce directly the pdf file.

Let's say your file name (without the file type extension) is : 'figure_output'

Under Matlab, you can type the following:

filo = 'img/figure_output'; 

print(gcf,'-depsc2',sprintf('%s.eps',filo));

system(sprintf('ps2pdf -dEPSCrop %s.eps %s.pdf',filo,filo));

This will create the eps file with Matlab and the pdf file with the system command 'ps2pdf'.

I'm reporting this little trick here because I've been looking for the '-dEPSCrop' option to ps2pdf for a while now ! 

Without this option the pdf file is of the default paper size of the ps2pdf command.

On a daily basis I use three Matlab functions to manage my figures.

- figure_land/figure_tall: to set the figure's orientation and paper size correctly (this correspond to setting A4 landscape or portrait formats). It also modifies the figure's size and color, so that it looks more like what is expected.

- exportp: this function allows one to simply manage figure export to pdf. Simply put it the figure's handle, a file name (no extension) and an orientation. This allows to export clean pdf to portrait, landscape and 'crop' formats