Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 474
Liw Avatar asked Mar 10 '11 07:03

Liw


2 Answers

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.

like image 66
ramanan Avatar answered Nov 15 '22 05:11

ramanan


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.

like image 20
AndresR Avatar answered Nov 15 '22 04:11

AndresR