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

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

This question is related to matlab save figure

The answer is


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

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


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

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


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


try plot(var); saveFigure('title'); it will save as a jpeg automatically


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


Examples related to matlab

how to open .mat file without using MATLAB? SQL server stored procedure return a table Python equivalent to 'hold on' in Matlab Octave/Matlab: Adding new elements to a vector How can I make a "color map" plot in matlab? How to display (print) vector in Matlab? Correlation between two vectors? How to plot a 2D FFT in Matlab? How can I find the maximum value and its index in array in MATLAB? How to save a figure in MATLAB from the command line?

Examples related to save

What is the difference between --save and --save-dev? Basic http file downloading and saving to disk in python? Save byte array to file Saving Excel workbook to constant path with filename from two fields PHP - get base64 img string decode and save as jpg (resulting empty image ) How to edit/save a file through Ubuntu Terminal Write and read a list from file How to do a "Save As" in vba code, saving my current Excel workbook with datestamp? How to dump raw RTSP stream to file? Saving images in Python at a very high quality

Examples related to figure

Error in plot.new() : figure margins too large, Scatter plot Python Matplotlib figure title overlaps axes label when using twiny How to save a figure in MATLAB from the command line? Matplotlib different size subplots How to use matplotlib tight layout with Figure? Matplotlib (pyplot) savefig outputs blank image matplotlib savefig in jpeg format How do I tell Matplotlib to create a second (new) plot, then later plot on the old one? In Matplotlib, what does the argument mean in fig.add_subplot(111)? How to force two figures to stay on the same page in LaTeX?