MATLAB: print a figure to pdf as the figure shown in the MATLAB

Liw picture Liw · Mar 10, 2011 · Viewed 36.4k times · Source

I am trying to export (save as, print) a figure into .pdf format. However, no matter how I configure the setting, there are large margins around the figure.

When I export the figure into .eps format, there is no such problem --- i.e. the figure just looks like it is displayed in the MATLAB.

How could I export the figure into .pdf format, which looks the same as it is shown in the MATLAB?

Answer

ramanan picture ramanan · Sep 18, 2013

You can automate the process above by adding the following lines of code immediately after the plot command.

set(gcf,'Units','inches');
screenposition = get(gcf,'Position');
set(gcf,...
    'PaperPosition',[0 0 screenposition(3:4)],...
    'PaperSize',[screenposition(3:4)]);
print -dpdf -painters epsFig

The first two lines measure the size of your figure (in inches). The next line configures the print paper size to fit the figure size. The last line uses the print command and exports a vector pdf document as the output.