How to make and save image for each iteration in a for loop?

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

Here you export the plot as JPEG image; are you talking about another image in your question? ..like an image that contains the loop index (written/rendered as pixels)?
Oh...okay I guess that test code isn't really a good one.
In my code, at each iteration I'll have an image of a matrix, and I want to save that as JPG image so I can make an animation later.
So you want to be able to generate an image a bit like this one? :
I'll have something like A = [445 356 435; 222 222 655; 222 435 543] ;
Then second most frequent value is 435. Make 435 0 and else 1, thus B = [1 1 0; 1 1 1; 1 0 1] ;
Now I'll convert B into a black and white image. All this at the end of an iteration so after 100 iterations I'll have 100 black and white images.
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 (?)
Mm, let me be more clear on this. With my example matrix B, I want an image like:

Sign in to comment.

 Accepted Answer

I understand now; you'll want something like:
filename = sprintf('mat_img_%d.jpg', n) ;
imwrite(B, filename, 'png') ;
Note that you can resize the image if it is too small, e.g.
filename = sprintf('mat_img_%d.jpg', n) ;
B = imresize(B, 100, 'nearest') ; % 100 is the scale factor.
imwrite(B, filename, 'png') ;
Then you can use the code point at by Image Analyst for building the movie.

3 Comments

This is indeed saving an image at each iteration, but all the images are white and I get one figure showing up that has black spots (which is the type of images I wanted to be saved.)
I have:
for j=1:tmax
% ... a long code that is updating a matrix mat
almos = mat ;
% Identify the second most frequent entry
idx_tab = maxidx(almos) ;
french = idx_tab(2) ;
% Turn almos into a matrix of 0's and 1's
for nn=1:mat_size
for kk=1:mat_size
if almos(nn,kk) == french
almos(nn,kk) = 0 ;
else
almos(nn,kk) = 1 ;
end
end
end
% Turn almos_mov into a black and white image of almos
almos_mov = imshow(almos,'InitialMagnification', 400) ;
% Save almos_mov
filename = sprintf('mat_img_%d.jpg', j) ;
imwrite(almos_mov, filename, 'jpg') ;
end
Did I make a mistake here?
Actually, you could avoid the double loop and replace it with:
img = ones(size(almos)) ;
img(almos==french) = 0 ;
Then you can proceed as follows:
filename = sprintf('mat_img_%d.jpg', n) ;
img = imresize(img, 400, 'nearest') ;
imwrite(img, filename, 'png') ;
The mistake that you made is that you ask IMSHOW to magnify what it displays, but this function doesn't return the magnified image (it only displays it magnified). The number that is returned is a handle to the image object actually. This is why I proposed IMRESIZE, which outputs a magnified image.
It didn't work with 400 as it said that the images were too big, so I reduced it to 200 and now it works. Thanks a million! :)

Sign in to comment.

More Answers (1)

3 Comments

I'm more struggling with creating an image and saving it at each iteration in a for loop. I was planning to make a stop motion movie of the images with iMovie, because I know how to do that.
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.
I'm going to take a look at how to make a movie with MATLAB after I finish this project...! Thanks a lot! :)

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!