在回答问题Matlab figure to pdf: measuring accuracy并从答案Limit of figure dimensions中学习时,另一个问题已经出现。
如果要超过限制时要保护要调整大小的数字?
答案 0 :(得分:0)
假设我们使用matlab 2011b,屏幕分辨率为1400 x 900像素,分辨率为96 ppi,我们想要输出尺寸为10“x 20”的数字,这肯定会超出限制。
FigureSize=[10 20];
FigureInchSize=FigureSize.*1; %\ Convert the given size to inches
ScrSize=get(0,'ScreenSize');
ScrSize=ScrSize(3:4);
PPI_def=get(0,'ScreenPixelsPerInch');
PPI_new=PPI_def;
%\\ Calculate the appropriate resolution PPI_new
if FigureSize(1)*PPI_new>ScrSize(1) %\ Will the figure width exceed the limit?
PPI_new=floor(ScrSize(1)/FigureInchSize(1));
end
if FigureSize(2)*PPI_new>ScrSize(2) % Will the figure height exceed (new) limit?
PPI_new=floor(ScrSize(2)/FigureInchSize(2));
end
set(0,'ScreenPixelsPerInch',PPI_new);
set(FigureHandle,'position',[0.1,0.1,FigureSize]);
%\\ Export the figure
export_fig('Foo','-pdf','-nocrop');
%\\ Reset the resolution
set(0,'ScreenPixelPerInch',PPI_def);
在第一部分中,我们读取了必要的值并将它们转换为合适的格式。我们还避免通过set(Handle,'Units',<Units>)
自动转换,这可能会干扰Position
值的解释。
在第二部分中,我们会根据需要更改分辨率值。
在第三部分中,我们更改分辨率,调整大小并导出图形,并将分辨率恢复为默认值。
当和如何定义图形的布局时,请小心。