MATLAB: print a figure to pdf as the figure shown in the MATLAB
Asked Answered
D

3

7

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?

Dallasdalli answered 10/3, 2011 at 7:5 Comment(2)
See How to save plot into PDF without large margin around.Roseline
Or also see Tight bounding box around PDF of MATLAB figureGospodin
E
3

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.

Endosteum answered 10/3, 2011 at 7:12 Comment(1)
It works nicely, thank you very much. Is it possible to save these settings to default? It seems that I have to do all these steps every time.Dallasdalli
G
7

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.

Gelatinate answered 18/9, 2013 at 9:46 Comment(3)
the figure seems blurry.Diaeresis
This code no longer seems to work in more recent versions of Matlab.Efficient
This code seems to work for me in Matlab R2021b, as long as the last line of code is removed.Horde
E
3

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.

Endosteum answered 10/3, 2011 at 7:12 Comment(1)
It works nicely, thank you very much. Is it possible to save these settings to default? It seems that I have to do all these steps every time.Dallasdalli
H
0

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

Himeji answered 5/6, 2019 at 8:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.