How to make and save image for each iteration in a for loop?
Show older comments
Dear all,
I want to know how to make and save an image for each time iteration in a for loop. The following is a simple test code I made, but if I can figure this out I believe mine should be easy to figure out too.
function hel = gr(st)
t = [1:10] ;
for n=1:10
graze(n,1) = (n+1)*st ;
graze(n,2) = (n+1)*(st+1) ;
end
figure(n)
hel = plot(t,graze(:,1),'r+',t,graze(:,2),'b+') ;
legend('ubc','my mood')
xlabel('time')
ylabel('hungry')
% filename = sprintf('mat_img_%d.jpg', n);
% saveas(gcf, filename, 'jpg')
end
every iteration, I'd like to create an image corresponding to the iteration step and save it on my computer. How should I modify this code? Thank you.
6 Comments
Edgaris Rhomer
on 23 Apr 2013
Cedric
on 23 Apr 2013
So you want to be able to generate an image a bit like this one? :

Edgaris Rhomer
on 23 Apr 2013
Cedric
on 23 Apr 2013
Yes, my example was about the image (if you right click on it, you can see that it is an image). So your question is how to produce this kind of image from a variable's content (?)
Edgaris Rhomer
on 23 Apr 2013
Accepted Answer
More Answers (1)
Image Analyst
on 23 Apr 2013
2 votes
3 Comments
Edgaris Rhomer
on 23 Apr 2013
Image Analyst
on 23 Apr 2013
If you want images, then use im2frame(). If you want the whole figure including axes, titles, tick marks, etc. then save each figure out with export_fig() to an image file, such as a PNG file, then build the movie by reading in all the frames with imread(), convert to a movie frame with im2frame. See the end of my demo here http://www.mathworks.com/matlabcentral/answers/48797#answer_59652 for an example using the standard demo movie rhinos that ships with MATLAB.
Edgaris Rhomer
on 23 Apr 2013
Categories
Find more on Timing and presenting 2D and 3D stimuli in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!