Matlab数字转pdf:测量精度 [英] Matlab figure to pdf: measuring accuracy

查看:74
本文介绍了Matlab数字转pdf:测量精度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过保存从以下Matlab代码生成的图形来生成PDF.当x=4时,它使用PDF测量工具生成了一个尺寸恰好为4英寸的正方形.但是,当x=5出现问题时,生成的PDF将失去准确性–请参见下图.

I am generating a PDF by saving a figure generated from the following Matlab code. When x=4, it generated a square whose measure is exactly 4 inch, using a PDF measure tool. But when x=5, something went wrong and the generated PDF loses accuracy – see the images below.

我正在尝试绘制一个准确的正方形(其PDF测量工具的尺寸与x定义的大小相同),以使打印正方形的中心和美国字母尺寸页面的中心(8.5"x 11") )完全匹配.

I am trying to draw an accurate square (whose PDF measure tool measurements are the same size as defined by x), such that center of the printed square, and the center of a US letter size page (8.5" x 11") matches exactly.

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英寸.正方形距左右,顶部和底部的距离相等.

When x=4, the Measure tool says 4 inch. Square is equally far apart from right and left, and from top and bottom.

x=5时,测量"工具显示5.47英寸,并且正方形向右和向右移动更多.

When x=5 the Measure tool says 5.47 inch, and the square is shifted more towards the right and bottom.

推荐答案

根据最新评论,您可以尝试以下代码:

According to last comment you can try this code:

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

我已经更新了代码,因此没有错误.

I have updated the code so it is error-free.

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

For my setup (Win7 Enterprise 32bit, Matlab 2011b, GhostSript) the resulting pdf is as follows:

您可以看到轴位置在8.5"x 11"纸张的中心.

You can see that axes position is in the centre of 8.5" x 11" paper.

但是,纸张高度限制为9.94英寸,非常接近我的屏幕高度.对于9.9英寸以下的高度,它可以正常工作.

However, the paper height is limitted to 9.94" which is fishily close to height of my screen. For heights below 9.9" it works without any problem.

编辑2

此处所述,较旧版本的Matlab(2014年及更低)的图形尺寸仅限于屏幕分辨率(;以像素为单位),并且在已知像素密度(PPI=get(0,'ScreenPixelsPerInch'))的情况下,我们可以计算以英寸为单位的限制(Limits=RES(2:3)./PPI).

As answered here, older versions of Matlab (2014 and below) have figure size limited to screen resolution (RES=get(0,'ScreenSize'); measured in pixels) and with known pixel density (PPI=get(0,'ScreenPixelsPerInch')) we can calculate limits in inches (Limits=RES(2:3)./PPI).

所以我试图牺牲分辨率:

So I tried to sacrify the resolution:

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)

这篇关于Matlab数字转pdf:测量精度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆