MATLAB: print a figure to pdf as the figure shown

2019-01-23 19:39发布

问题:

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?

回答1:

You can try the following:

1) After you plot the figure in MATLAB, go to 'File->Export Setup', and input the size of the output you want. For example, Width: 6 inches, Height: 5 inches. Then click 'Apply to Figure' button.

2) Don't close the 'Export Setup' window. Go to 'File->Print Preview->Paper', input the same size in the Width and Height options.

3) Don't close the 'Print Preview' window. Go back to the 'Export Setup' window, and click 'Export', then select pdf format and save it.

4) Check the output PDF file, you'll see it is perfect.

I found the solution in blog post Export figure to PDF in MATLAB.



回答2:

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.