Export Matlab figure as PNG?

后端 未结 5 1908
小鲜肉
小鲜肉 2020-12-06 17:22

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]); % background         


        
5条回答
  •  半阙折子戏
    2020-12-06 17:44

    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!

提交回复
热议问题