how to save the content of the current figure as an image?

438 views (last 30 days)
how to save the content(which is an image) of the current figure as an image?
I have about 30 figure ...
I want to use MATLAB commands to save them in for loop

Accepted Answer

Mohammad Abouali
Mohammad Abouali on 19 Sep 2014
Edited: Mohammad Abouali on 19 Sep 2014
saveas(gcf,'filename.png')
would also work.
or something like this
for i=1:30
% code to show image number i
saveas(gcf,['filename' num2str(i) '.png']);
end
if you don't like the border around it type:
iptsetpref('ImshowBorder','tight');
before preparing your figure.
  3 Comments
Mohammad Abouali
Mohammad Abouali on 26 Sep 2014
Edited: Mohammad Abouali on 26 Sep 2014
Cool image. What are these? blood stream?
Anyway, so you can use fullfile() command. Something like this should work
DirectoryPath ='C:\Mystorage\myImages';
whereToStore=fullfile(DirectoryPath,['filename' num2str(i) '.png']);
saveas(gcf, whereToStore);
If you wanna make it more fancy you can have the user select the folder through a GUI like this
DirectoryPath = uigetdir();
if DirectoryPath~=0
whereToStore=fullfile(DirectoryPath,['filename' num2str(i) '.png']);
saveas(gcf, whereToStore);
end

Sign in to comment.

More Answers (3)

Francesco Ciriello
Francesco Ciriello on 20 Nov 2019
Save current figure
imwrite(getframe(gcf).cdata, 'myfilename.png')
Save current axis
imwrite(getframe(gca).cdata, 'myfilename.png')
  1 Comment
Raviteja Vangara
Raviteja Vangara on 12 May 2020
I think this deserves an upvote. I have an image with alpha data, and traditional saveas options wouldnt store alpha data in saved PNG file.
using,
H = getframe(gca)
imwrite(H.cdata, 'myfilename.png')
This saves current figure as it appears on the window.

Sign in to comment.


Mikhail
Mikhail on 18 Sep 2014
If you want to save in with your hands - just click file-> save figure.
  1 Comment
Nisreen Sulayman
Nisreen Sulayman on 18 Sep 2014
I have about 30 figure ...
I want to use MATLAB commands to save them in for loop

Sign in to comment.


Image Analyst
Image Analyst on 18 Sep 2014
Use imwrite() if you just want to save the image you display. If you want to save the axes or figure, use export_fig(). http://matlab.wikia.com/wiki/FAQ#How_do_I_save_my_figure.2C_axes.2C_or_image.3F_I.27m_having_trouble_with_the_built_in_MATLAB_functions.
  2 Comments
Image Analyst
Image Analyst on 19 Sep 2014
Just use fullfile() when you construct the file name that you pass in to export_fig().

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!