close all
drawnow ()
pdffile = sprintf ('%s.pdf', mfilename ());
ppi = 90;
dpi = 129;
screenpixelsperinch = get (0, 'screenpixelsperinch');
actual_screenpixelsperinch = 129;
foo = ver;
if (strcmpi (foo(1).Name, 'Octave'))
  graphics_toolkit qt
  pdffile = strrep (pdffile, 'matlab', 'octave');
  % Octave reports pixels/inch to be 129.
  scale_figure_size = actual_screenpixelsperinch / screenpixelsperinch;
  scale_font_size = ppi / dpi;
else
  % Matlab reports pixels/inch to be 90, not 129.
  scale_figure_size = actual_screenpixelsperinch / screenpixelsperinch;
  scale_font_size = ppi / dpi;
end

width = 4;
height = 3;
figure (1)
set (gcf (), 'units', 'inches')
set (gcf (), 'paperunits', 'inches')
position = get (gcf (), 'position');
position(3:4) = [width, height] * scale_figure_size;
paperposition = [0, 0, width, height];
papersize = [width, height];
set (gcf (), 'papersize', papersize)
set (gcf (), 'position', position)
set (gcf (), 'paperposition', paperposition)
plot (rand (3))
xlabel xlabel
ylabel ylabel
title 123456789012345678900123456789012345678901234567890
text (0.95, 0.95, foo(1).Name, 'units', 'normalized', ...
  'verticalalignment', 'top', 'horizontalalignment', 'right')
drawnow ()
h = findall (gcf (), '-property', 'fontsize');
set (h, 'fontsize', 12)
fontsize = cell2mat (get (h, 'fontsize'));
for n = 1:numel(h)
  set (h(n), 'fontsize', fontsize(n) * scale_font_size)
end
print ('-dpdfwrite', pdffile)
for n = 1:numel(h)
  set (h(n), 'fontsize', fontsize(n))
end
system (sprintf ('open %s', pdffile));
final_position = get (gcf (), 'position');
fprintf ('By printing, the figure size was scaled by (%fx%f)\n', final_position(3:4) ./ position(3:4))

fprintf ('Title vertical alignment = %s\n', ...
  get (get (gca (), 'title'), 'verticalalignment'));
fprintf ('XLabel vertical alignment = %s\n', ...
  get (get (gca (), 'xlabel'), 'verticalalignment'));
fprintf ('YLabel vertical alignment = %s\n', ...
  get (get (gca (), 'ylabel'), 'verticalalignment'));
  
