Matlab figure to pdf: measuring accuracy

前端 未结 1 1597
一个人的身影
一个人的身影 2021-01-21 21:28

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 meas

相关标签:
1条回答
  • 2021-01-21 22:00

    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
    

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

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

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

    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.

    EDIT 2

    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)
    
    0 讨论(0)
提交回复
热议问题