Matlab图到pdf:测量精度

时间:2016-03-30 11:23:07

标签: matlab pdf

我通过保存从以下Matlab代码生成的数字来生成PDF。当x=4时,它使用PDF测量工具生成一个正方形,其度量正好为4英寸。但是当x=5出现问题时,生成的PDF会失去准确性 - 请参阅下面的图片。

我正在尝试绘制一个精确的正方形(其PDF测量工具的尺寸与x定义的尺寸相同),例如印刷正方形的中心和美国字母尺寸页面的中心(8.5“x 11” )完全匹配。

clear all
close all   
x=4;
plot([0 x  x 0], [0 0 x x]), axis tight
% set(gca, 'Position',[0.1 0.1 .8 .8])
set(gca, 'Units','inches', 'Position',[1 1 x x])
set(gcf, 'Units','inches', 'Position',[0 0 x+2 x+2])
% set(gcf, 'PaperUnits','inches', 'PaperPosition',[0 0 8.5 11])

x=4时,测量工具显示4英寸。广场距离左右,顶部和底部都差不多。

correct size

x=5测量工具显示5.47英寸时,方块向右和底部移动更多。

different size

1 个答案:

答案 0 :(得分:2)

根据上次评论,您可以尝试以下代码:

clear all,close all   
x=5;
PaperSize=[8.5 11];
if x<min(PaperSize)
    plot([0 x  x 0], [0 0 x x]), axis tight
    %\\ Set the figure dimensions to US letter and change the background
    set(gcf,'units','inches','Position',[0.1 0.1 PaperSize],'color','w')
    %\\ Set axis position to the center
    set(gca, 'Units','inches', 'Position',[(PaperSize-[x x])./2 x x])
    export_fig('Foo','-pdf','-nocrop')

else
    disp('Axes too wide, man')
end

修改 我已经更新了代码,因此它没有错误。

对于我的设置(Win7 Enterprise 32位,Matlab 2011b,GhostSript),得到的pdf如下:

enter image description here

您可以看到轴位置位于8.5“x 11”纸的中心。

然而,纸张高度限制为9.94“,这与我的屏幕高度接近。对于高度低于9.9”,它没有任何问题。

编辑2

在回答here时,旧版本的Matlab(2014及以下版本)的图形大小仅限于屏幕分辨率(RES=get(0,'ScreenSize');以像素为单位测量)和已知像素密度(PPI=get(0,'ScreenPixelsPerInch'))我们可以用英寸(Limits=RES(2:3)./PPI)来计算限制。

所以我试图牺牲决议:

clear all,close all   
x=5;
PaperSize=[8.5 20];  %\\ Paper Height set to 20" for example
PPI_def=get(0,'ScreenPixelPerInch');
Scr=get(0,'ScreenSize');
Scr_H_px=Scr(4);     %\\ Read the screen height in pixels
PPI_new=floor(Scr_H_px/PaperSize(2)); %\ Count new resolution
if PPI_new>PPI_def   %\\ Chech if we do not need to change the resolution
  PPI_new=PPI_def;
end
%%\\ Set root (0) resolution from default 96 to lower
set(0,'ScreenPixelPerInch',PPI_new)
if x<min(PaperSize)
  plot([0 x  x 0], [0 0 x x]), axis tight
  %\\ Set the figure dimensions to US letter and change the background
  set(gcf,'units','inches','Position',[0.1 0.1 PaperSize],'color','w')
  %\\ Set axis position to the center
  set(gca, 'Units','inches', 'Position',[(PaperSize-[x x])./2 x x])
  export_fig('Foo','-pdf','-nocrop')
else
  disp('Axes too wide, man')
end
%%\\ reset the resolution back
set(0,'ScreenPixelPerInch',PPi_def)