12

I need to automatically export figures from Matlab to PNG. My figure has a size of 600x200 px:

hFig = figure(1); 
set(hFig, 'Color', [1 1 1]); % backgroundcolor white
set(hFig, 'Position', [500 500 600 200]) % size 600x200

I tried e.g.

print -dpng image.png

but the image.png is larger than 600x200 px. Exporting the figure manually from the Figure Window GUI using the "save" button works great, I want to do exactly this automatically / from a script. Thanks for any hint!

stefan.at.kotlin
  • 15,347
  • 38
  • 147
  • 270

5 Answers5

15

I also know the problem that figures save never look the same as on screen.

There is the saveas command which might work for you - but does also some resolution changing for me.

Only way I know is to carefully set every aspect like this:

set(gcf,'PaperUnits','inches','PaperSize',[2,6],'PaperPosition',[0 0 2 6])
print('-dpng','-r100','test')

(so paper size is 2x6" and print with 100dpi, PaperPosition is important as you will have otherwise some extra border.)

bdecaf
  • 4,652
  • 23
  • 44
4

My preferred approach for generating png plots from MATLAB is the export_fig utility available at the MATLAB file exchange.

Here's an example:

set(gcf, 'Position', [100 100 500 500], 'Color', 'w')

x=0:0.01:10;
plot(x, sin(x))
set(gca, 'FontSize', 20, 'FontName', 'Arial')

export_fig 'strip-diff-far-forward.png' -painters -nocrop

This will create a png that is 500 x 500 pixels, with 20 pixel fonts. I'm sure that internally it does the same kinds of things as in bdecaf's answer, but it is all ecapsulated in a function for you already, and has a bunch of other features too.

The drawback is that if you use the -painters renderer (which I think looks the best) you will need to have ghostscript installed. If you don't want to mess with that, you can change -painters to -opengl

Edit Now setting figure size correctly!

Dan Becker
  • 1,196
  • 10
  • 18
1

Based upon bdecaf's answer:

set(gcf,'PaperUnits','inches','PaperSize',[600/96,200/96],'PaperPosition',[0 0 600/96 200/96])
print('-dpng','-r96','test')

96 is the dpi of my system. This gives me EXACTLY the same output as the save function. For Windows the dpi is typically 96, sometimes 120. Simply adjust it accordingly to your system. Note that on a beamer the DPI might again be different from your system, especially if your system has 120 DPI! 96 DPI should in general be a quite safe choice for beamers I think. Google if you need help finding out the DPI setting of your system. This answer is 99,9% based on bdecaf and Florian, so I will leave bdecaf's answer selected as the right one.

edit: 600 = horizontal image size in px, 200 = vertical image size in px

stefan.at.kotlin
  • 15,347
  • 38
  • 147
  • 270
1

Amro answer works perfectly, after you generate your figure, set PaperPositionMode to auto and the print size will be the same as screen size.

set(gcf, 'PaperPositionMode','auto')   
print('-dpng','test.png')
NG_
  • 6,895
  • 7
  • 45
  • 67
Reza
  • 11
  • 2
0

Try:

set(hFig, 'PaperPositionMode','auto')   %# WYSIWYG
print -dpng -r0 image.png               %# at screen resolution

This tells it to produce an image the same size as it appears on screen.

Amro
  • 123,847
  • 25
  • 243
  • 454