How to save a figure in MATLAB from the command line?

后端 未结 7 1069
灰色年华
灰色年华 2020-12-13 09:16

Is there a command in MATLAB which allows saving a figure in FIG or JPEG or both formats automatically?

相关标签:
7条回答
  • 2020-12-13 09:32

    When using the saveas function the resolution isn't as good as when manually saving the figure with File-->Save As..., It's more recommended to use hgexport instead, as follows:

    hgexport(gcf, 'figure1.jpg', hgexport('factorystyle'), 'Format', 'jpeg');
    

    This will do exactly as manually saving the figure.

    source: http://www.mathworks.com/support/solutions/en/data/1-1PT49C/index.html?product=SL&solution=1-1PT49C

    0 讨论(0)
  • imwrite(A,filename) writes image data A to the file specified by filename, inferring the file format from the extension

    0 讨论(0)
  • 2020-12-13 09:43

    If you want to save it as .fig file, hgsave is the function in Matlab R2012a. In later versions, savefig may also work.

    0 讨论(0)
  • 2020-12-13 09:44

    Use saveas:

    h=figure;
    plot(x,y,'-bs','Linewidth',1.4,'Markersize',10);
    % ...
    saveas(h,name,'fig')
    saveas(h,name,'jpg')
    

    This way, the figure is plotted, and automatically saved to '.jpg' and '.fig'. You don't need to wait for the plot to appear and click 'save as' in the menu. Way to go if you need to plot/save a lot of figures.

    If you really do not want to let the plot appear (it has to be loaded anyway, can't avoid that, else there is also nothing to save), you can hide it:

    h=figure('visible','off')
    
    0 讨论(0)
  • 2020-12-13 09:44

    These days (May 2017), MATLAB still suffer from a robust method to export figures, especially in GNU/Linux systems when exporting figures in batch mode. The best option is to use the extension export_fig

    Just download the source code from Github and use it:

    plot(cos(linspace(0, 7, 1000)));
    set(gcf, 'Position', [100 100 150 150]);
    export_fig test2.png
    
    0 讨论(0)
  • 2020-12-13 09:48

    I don't think you can save it without it appearing, but just for saving in multiple formats use the print command. See the answer posted here: Save an imagesc output in Matlab

    0 讨论(0)
提交回复
热议问题