Is there a command in MATLAB which allows saving a figure in FIG or JPEG or both formats automatically?
-
actually this is my plot 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'); and I would like to save it automatically without waiting for the plot to appear ! but it seems that it is not possible because I cannot define a variable of multi curves on the same figure – ABC-biophi Aug 28 '12 at 13:26
-
possible duplicate of.. a lot: http://stackoverflow.com/search?q=%5Bmatlab%5D+save+figure – Gunther Struyf Aug 28 '12 at 13:41
7 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')

- 11,158
- 2
- 34
- 58
-
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 Aug 28 '12 at 13:50
-
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](http://www.mathworks.nl/help/techdoc/matlab_prog/f10-60570.html) – Gunther Struyf Aug 28 '12 at 13:58
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

- 477
- 1
- 5
- 20

- 321
- 3
- 8
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
-
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 Aug 28 '12 at 13:49
-
1@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` – Malife Aug 28 '12 at 14:37
-
I lloked in this page, but I didn't find how to save it in ".fig" format http://www.mathworks.de/help/techdoc/ref/print.html – ABC-biophi Aug 29 '12 at 09:34
-
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 ??? – ABC-biophi Aug 29 '12 at 10:36
If you want to save it as .fig file, hgsave is the function in Matlab R2012a. In later versions, savefig may also work.

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

- 3,552
- 4
- 28
- 47
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

- 1,466
- 1
- 13
- 27
try plot(var); saveFigure('title'); it will save as a jpeg automatically

- 1
-
-
Try it and you will see that yes there is one, although 'savefig' is advised by a warning message. – Thom Mar 25 '14 at 14:12
-
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](http://www.mathworks.com/help/matlab/ref/savefig.html#bt0w30o-1_1)... – cedyd Mar 29 '14 at 16:09
-