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

153,496

Solution 1

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

Solution 2

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

Solution 3

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

Solution 4

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

Share:
153,496
ABC-biophi
Author by

ABC-biophi

Updated on May 27, 2020

Comments

  • ABC-biophi
    ABC-biophi almost 4 years

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

  • ABC-biophi
    ABC-biophi over 11 years
    I tried it already, but it shows this error ??? Error: File: CurvePlotter.m Line: 3 Column: 1 Unexpected MATLAB expression. it is because of the hold all inside my plot !
  • ABC-biophi
    ABC-biophi over 11 years
    I tried it already, but it shows this error ??? Error: File: CurvePlotter.m Line: 3 Column: 1 Unexpected MATLAB expression. it is because of the hold all inside my plot !
  • Gunther Struyf
    Gunther Struyf over 11 years
    and what is there at line3 in that file? You know.. hold all is a command by itself, don't use it as hold all plot(Qx,Qy,'-.r*','Markersize',8) but rather hold all; plot(...) Also: learn to debug
  • Malife
    Malife over 11 years
    @Abdullah I'm confused since you posted exactly the same comment to both answers. Do you get this error using the print command or using saveas? Regardless of which one you get the error with, I have this code working: x = 1:100; y = rand(1,100); Qx = x; Qy = rand(1,100); plot(x,y,'-bs','Linewidth',1.4,'Markersize',10) hold all plot(Qx,Qy,'-.r*','Markersize',8) title('Curve of the protein strand','FontSize',14); legend('P Points (Input)','Q points (Output)','Location','North'); print(gcf, '-djpeg99', num2str(1)); hold
  • ABC-biophi
    ABC-biophi over 11 years
    I lloked in this page, but I didn't find how to save it in ".fig" format mathworks.de/help/techdoc/ref/print.html
  • ABC-biophi
    ABC-biophi over 11 years
    ok .. I got it ! plot(x,y,'-bs','Linewidth',1.4,'Markersize',10) hold all plot(Qx,Qy,'-.r*','Markersize',8) title('Curve of the protein strand','FontSize',14); legend('P Points (Input)','Q points (Output)','Location','North'); print(gcf, '-djpeg99', num2str(1)); saveas(gcf,'1'); you get out 2 outputs, one as jpg and the other as .fig but still I don't understand what does 'gcf' mean ???
  • Gastón Bengolea
    Gastón Bengolea about 10 years
    there isn't any function named like that
  • Thom
    Thom about 10 years
    Try it and you will see that yes there is one, although 'savefig' is advised by a warning message.
  • Admin
    Admin about 10 years
    There is one indeed... like Thom said. but I need to correct myself you will need to do saveFigure('title.jpg'); but I also saw savefig online...
  • Franklin Yu
    Franklin Yu over 7 years
    That's savefig not saveFigure.