2

I'm trying to use the answers I found in these questions:

to print a matlab plot to pdf without having the white margins included.

However using this code:

function saveTightFigure( h, outfilename, orientation )

% SAVETIGHTFIGURE(H,OUTFILENAME) Saves figure H in file OUTFILENAME without
%   the white space around it. 
%
% by ``a grad student"
% http://tipstrickshowtos.blogspot.com/2010/08/how-to-get-rid-of-white-margin-in.html

% get the current axes
ax = get(h, 'CurrentAxes');

% make it tight
ti = get(ax,'TightInset');
set(ax,'Position',[ti(1) ti(2) 1-ti(3)-ti(1) 1-ti(4)-ti(2)]);

% adjust the papersize
set(ax,'units','centimeters');
pos = get(ax,'Position');
ti = get(ax,'TightInset');
set(h, 'PaperUnits','centimeters');
set(h, 'PaperSize', [pos(3)+ti(1)+ti(3) pos(4)+ti(2)+ti(4)]);
set(h, 'PaperPositionMode', 'manual');
set(h, 'PaperPosition',[0 0  pos(3)+ti(1)+ti(3) pos(4)+ti(2)+ti(4)]);

% save it
%saveas(h,outfilename);
if( orientation == 1)
    orient portrait
else
    orient landscape
end
print( '-dpdf', outfilename );

end

Results in this output:

enter image description here

As you can see the 'PaperSize' seems to be set not properly. Any idea of possible fixes?

NOTE

If I change the orientation between landscape and portrait the result is the same, simply the image is chopped in a different way.

However if I save the image with the saveas(h,outfilename); instruction the correct output is produced.

Why is this? And what is the difference between the two saving instructions?

Community
  • 1
  • 1
Matteo
  • 7,924
  • 24
  • 84
  • 129
  • 1
    I finally gave up on trying to print to pdf directly and started printing to eps, which I can easily convert to pdf when needed. – nispio Nov 23 '13 at 21:09

1 Answers1

2

Alltogether the answers you mentioned offer a lot of approaches, but most of them didn't worked for me neither. Most of them screw up your papersize when you want to get the tight inset, the only which worked for me was:

set(axes_handle,'LooseInset',get(axes_handle,'TightInset'));

I finally wrote a function, where I specify the exact height and width of the output figure on paper, and the margin I want (or just set it to zero). Be aware that you also need to pass the axis handle. Maybe this functions works for you also.

function saveFigure( fig_handle, axes_handle, name , height , width , margin)

set(axes_handle,'LooseInset',get(axes_handle,'TightInset'));
set(fig_handle, 'Units','centimeters','PaperUnits','centimeters')

% the last two parameters of 'Position' define the figure size
set(fig_handle,'Position',[-margin -margin width height],...
       'PaperPosition',[0 0 width+margin height+margin],...
       'PaperSize',[width+margin height+margin],...
       'PaperPositionMode','auto',...
       'InvertHardcopy', 'on',...
       'Renderer','painters'...     %recommended if there are no alphamaps
   );
saveas(fig_handle,name,'pdf')

end

Edit: if you use painters as renderer saveas and print should produce similar results. For jpegs print is preferable as you can specify the resolution.

Robert Seifert
  • 25,078
  • 11
  • 68
  • 113
  • For `axes_handle` I use, e.g., `h=gca;`, but how can I specify the name? – AboAmmar Jul 31 '15 at 18:04
  • @AboAmmar I don't get your question. – Robert Seifert Aug 01 '15 at 18:52
  • ,@thewaywewalk Yes I named it `file.pdf` and went OK, but for older versions of matlab (I tested 2009b), it crops the figure to the given `height , width` starting from the bottom left corner loosing information. In Matlab 2015a, however, it worked well, but the right margin is too tight that the markers on the edge are cut. – AboAmmar Aug 01 '15 at 19:09
  • Also is it possible to increase the resolution of the output pdf figure? I zoom a little and see the circle markers as a non-perfect polygon. – AboAmmar Aug 01 '15 at 19:11
  • @AboAmmar with R2014b everything changed. Any maybe this answer does not apply anymore. – Robert Seifert Aug 01 '15 at 20:09