5

When creating a simple figure in MATLAB and saving it as PDF, the resulting PDF file will have a luxurious bounding box.

plot(1,1,'x')
print(gcf, '-dpdf', 'test.pdf');

(From the ratio of the output it seems they always put in on an A page.)

Is there a simple way to get a tight bounding box around the PDF?

4

2 回答 2

4

您可以按如下方式格式化边界框

figure(1)
hold on;
plot(1,1,'x')

ps = get(gcf, 'Position');
ratio =  ps(4) / ps(3)
paperWidth = 10;
paperHeight = paperWidth*ratio;


set(gcf, 'paperunits', 'centimeters');
set(gcf, 'papersize', [paperWidth paperHeight]);
set(gcf, 'PaperPosition', [0    0   paperWidth paperHeight]);


print(gcf, '-dpdf', 'test2.pdf');

对于较小的边框,您可以调整paperposition属性,例如

set(gcf, 'PaperPosition', [-0.5   -0.5   paperWidth+0.5 paperHeight+0.5]);

~编辑~

正如 Space47 的回答所指出的,我更正了比率的计算,因为它是错误的。(感谢@Space47)。

于 2012-08-28T14:18:02.857 回答
3

一个老问题,但我会回答,因为谷歌在 Mathworks 自己的帮助页面之前为我找到了这个(对不起,没有足够的声誉来发表评论到以前)。反正

ratio = (ps(4)-ps(2)) / (ps(3)-ps(1))

应该

ratio = ps(4)/ps(3);

作为第一个值 gcf.Position 是屏幕上的 [x,y] 位置,与大小无关。

Matlab(R) 也给出了答案,特别是如果您不想/不需要调整图形大小: https://se.mathworks.com/help/matlab/creating_plots/save-figure-with-minimal-white-space。 html

fig = gcf;
fig.PaperPositionMode = 'auto'
fig_pos = fig.PaperPosition;
fig.PaperSize = [fig_pos(3) fig_pos(4)];
于 2017-12-20T09:32:08.420 回答