2

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.

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])

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

correct size

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

different size

Jongware
  • 22,200
  • 8
  • 54
  • 100
kaka
  • 155
  • 2
  • 8
  • why `x+2`? Should the `2` not change? Maybe to be `x/2` instead? – Dan Mar 30 '16 at 11:30
  • I added 2 just to give keep the gca boundary, 1 inch far from gcf boundary both from horizontal and vertical directions. – kaka Mar 30 '16 at 11:35
  • You need to add more details to your question. Firstly what does "pdf loses accuracy" mean. Explain this in detail. Secondly, have you tried other numbers for `x` besides `4` and `5`? Are any other numbers working or only `4`? Also, don't use code tags for emphasis, it's confusing for your readers. – Dan Mar 30 '16 at 11:37
  • I have added the pdf snaps, to define what I mean by accuracy. – kaka Mar 30 '16 at 11:42
  • If you **don't need** the resulting pdf to have the standard dimensions, you can use `export_fig` directly. If you do, set the `gcf`and `gca` `'position'` to fit your needs and use `export_fig`. http://www.mathworks.com/matlabcentral/fileexchange/23629-export-fig – Crowley Mar 30 '16 at 11:52
  • I direly need to have a pdf with the exact dimensions. Thanks for your export idea, though. – kaka Mar 30 '16 at 11:58

1 Answers1

2

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:

enter image description here

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)
Community
  • 1
  • 1
Crowley
  • 2,279
  • 6
  • 22
  • 36
  • problem is still the same. Wrong output pdf. – kaka Mar 30 '16 at 13:42
  • The pdf I get from the proposed solution is still have an inaccurate measurement issue. – kaka Mar 30 '16 at 15:27
  • Ok. You have updated the answer. I ll look at it sometime soon. Thanks – kaka Mar 30 '16 at 15:28
  • I've added `'-nocrop'` parameter which avoid the white space on the edges to be cropped. That was, perhaps, the "inaccurate measurement issue". – Crowley Mar 30 '16 at 15:42
  • Everything goes well, but the output Foo.pdf has got wrong size of 8.5"x8.57" instead of 8.5x11". windows 7, 32 Bit, Matlab R2013a. – kaka Mar 31 '16 at 09:29
  • Thanks for posing another solution. Before I tried the new solution, I found this answer to be very easy understanding and elegant. http://stackoverflow.com/questions/7561999/how-to-set-the-plot-in-matlab-to-a-specific-size/7576598#7576598 – kaka Mar 31 '16 at 14:39