title: save figure with subplots as a PDF
body: <p>I am using the following code to convert my MATLAB figures into PDF as it does a great job removing the unnecessary margins and I can use it inside loops. However, when I have figures with subplots it doesn't work because the code uses only the current axis. I haven't been able to generalize the code in the way I want. Appreciate any help.</p>
  
  <pre><code>ax = gca;
  set(ax,'fontname','Times New Roman','fontsize',12)
  ti = get(ax,'TightInset');
  set(ax,'Position',[ti(1) ti(2) 1-ti(3)-ti(1) 1-ti(4)-ti(2)]);
  set(ax,'units','centimeters')
  pos = get(ax,'Position');
  ti = get(ax,'TightInset');
  set(gcf, 'PaperUnits','centimeters');
  set(gcf, 'PaperSize', [pos(3)+ti(1)+ti(3) pos(4)+ti(2)+ti(4)]);
  set(gcf, 'PaperPositionMode', 'manual');
  set(gcf, 'PaperPosition',[0 0 pos(3)+ti(1)+ti(3) pos(4)+ti(2)+ti(4)]);
  saveas(gcf,['sample.pdf'])
  </code></pre>