function print2pdf(h,fileName)

% Print figure to PDF. Function takes care of paper sizing so figure 
% doesn't get cut off when printing to PDF (i.e., PDF is exactly what you 
% have in your MATLAB figure). The function defults to 300 dpi.
% 
% Inputs: 
% h = Axes handle (e.g., gcf)
% fileName = name and path foe output file (e.g., '~/Documents/name')
% 
% Last modified April 07, 2015
% Ben Raanan


set(h,'Unit','Inches','PaperUnits','Inches','PaperPositionMode','auto');
P = get(h,'Position');
set(h, 'PaperSize', [P(3) P(4)],'PaperPosition',[0 0 P(3) P(4)]);

print(fileName, '-opengl', '-r300', '-dpng') 
display([fileName ': Printing complete!'])  