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

1 visualización (últimos 30 días)
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 comentarios
Cedric
Cedric el 23 de Abr. de 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
Edgaris Rhomer el 23 de Abr. de 2013
Mm, let me be more clear on this. With my example matrix B, I want an image like:

Iniciar sesión para comentar.

Respuesta aceptada

Cedric
Cedric el 23 de Abr. de 2013
Editada: Cedric el 23 de Abr. de 2013
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 comentarios
Cedric
Cedric el 23 de Abr. de 2013
Editada: Cedric el 23 de Abr. de 2013
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.
Edgaris Rhomer
Edgaris Rhomer el 23 de Abr. de 2013
Editada: Edgaris Rhomer el 23 de Abr. de 2013
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! :)

Iniciar sesión para comentar.

Más respuestas (1)

Image Analyst
Image Analyst el 23 de Abr. de 2013
  3 comentarios
Image Analyst
Image Analyst el 23 de Abr. de 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
Edgaris Rhomer el 23 de Abr. de 2013
I'm going to take a look at how to make a movie with MATLAB after I finish this project...! Thanks a lot! :)

Iniciar sesión para comentar.

Categorías

Más información sobre Timing and presenting 2D and 3D stimuli en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by