Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 643
ABC-biophi Avatar asked Aug 28 '12 13:08

ABC-biophi


People also ask

How do I save a figure in MATLAB?

Click File > Generate Code.... The generated code displays in the MATLAB Editor. Save the code by clicking File > Save As. Generated files do not store the data necessary to recreate the graph, so you must supply the data arguments.

How do I save a figure property in MATLAB?

The best method for storing a figure's properties, then resetting them at a later time, is to use the SAVEAS function to save the figure as a FIG-file.

How do I export an image from MATLAB?

To export data from the MATLAB® workspace using one of the standard graphics file formats, use the imwrite function. Using this function, you can export data in formats such as the Tagged Image File Format (TIFF), Joint Photographic Experts Group (JPEG), and Portable Network Graphics (PNG).

How do I save open figures in MATLAB?

Open Figure Saved in MATLAB Figure FileCreate a surface plot and save the figure as a MATLAB figure file. Then, close the figure. Open the saved figure.


2 Answers

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') 
like image 178
Gunther Struyf Avatar answered Sep 28 '22 01:09

Gunther Struyf


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

like image 24
Avico Avatar answered Sep 28 '22 00:09

Avico