似乎将PaperPositionMode 设置为auto 将消除EPS 文件的多余空白,但不是PDF。
为了消除 PDF 的空白,我编写了一个小脚本来将纸张大小调整为图形大小。由于它很短,我将它包含在下面以防其他人需要它。
它的灵感来自this document,以及这个StackOverflow question。
我的解决方案通过仅操作纸张大小而不是图形轴来工作,因为操作轴会遇到子图问题。这也意味着将保留 一些 空白。在 MATLAB 的词汇表中,这些数字的边界是 OuterPosition,而不是 TightInset。
function [filename] = printpdf(fig, name)
% printpdf Prints image in PDF format without tons of white space
% The width and height of the figure are found
% The paper is set to be the same width and height as the figure
% The figure's bottom left corner is lined up with
% the paper's bottom left corner
% Set figure and paper to use the same unit
set(fig, 'Units', 'centimeters')
set(fig, 'PaperUnits','centimeters');
% Position of figure is of form [left bottom width height]
% We only care about width and height
pos = get(fig,'Position');
% Set paper size to be same as figure size
set(fig, 'PaperSize', [pos(3) pos(4)]);
% Set figure to start at bottom left of paper
% This ensures that figure and paper will match up in size
set(fig, 'PaperPositionMode', 'manual');
set(fig, 'PaperPosition', [0 0 pos(3) pos(4)]);
% Print as pdf
print(fig, '-dpdf', name)
% Return full file name
filename = [name, '.pdf'];
end