简体   繁体   中英

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

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 --- ie 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?

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.

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-lines script, for exporting to PDF in landscape A4 (assuming your plot is the "current figure"):

%-------------------------------------------------------------------

% resize paper as a landscape-A4 and reposition figure accordingly

set( gcf,'PaperSize',[29.7 21.0], 'PaperPosition',[0 0 29.7 21.0])

% export to PDF file 'YourFileName.pdf'

print -dpdf 'YourFileName'

%-------------------------------------------------------------------

Any other tweak : check the Figure properties - simply "get( gcf )" on the command window

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM