Search code examples
matlabgraphicspngexport

Export Matlab figure as PNG?


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!


Solution

  • 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.)