How to save figure in pdf without margins?

122 次查看(过去 30 天)
Let us suppose I have a figure with definite pixel size:
set(gcf,'unit','pixel','position',[0 0 figure_width figure_height])
I would like to save it as a pdf. I know the aspect ratio, but how to set the size? I don't want unnecessary margins. I want to use something like this
set(gcf,'PaperPositionMode','auto','papersize',[a*figure_width a*figure_height]);
%print(gcf,'Fig','-dpdf','-r0')
But how to choose the value of 'a' ?

回答(1 个)

Jan
Jan 2018-7-22
The value of a depends on the wanted size of the PDF. You can choose it freely to what you need. To remove the border use:
print(gcf, 'Fig.pdf', '-dpdf', '-fillpage')
  3 个评论
Jan
Jan 2019-12-9
Did you try the -loose flag in the print command?
'-loose' — Use a loose bounding box. EPS and PS files only.
André de Castro
André de Castro 2019-12-19
Using -loose did improve the results. It worked for PDF, EPS and PS files. I also had to:
  • not use -fillpage;
  • set the Position property before printing, as below.
width = 3.5;
height = 2.2;
figure_handle.Units = 'inches';
figure_position = figure_handle.Position;
figure_handle.Position = [figure_position(1),figure_position(2), width, height];
It seems strange that I had to set the figure Position though, as I had already set PaperUnits and PaperSize appropriately.
Combining -loose and the above code with the solution suggested here, in the Expand Axes to Fill Figure section, gave even better results, cropping as close as possible to the drawn area. Said solution was not effective when I had tried it before because I was using -fillpage.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Printing and Saving 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by