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

ABC-biophi picture ABC-biophi · Aug 28, 2012 · Viewed 152.1k times · Source

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

Answer

Gunther Struyf picture Gunther Struyf · Aug 28, 2012

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')