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

18 次查看(过去 30 天)
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 个评论
Cedric
Cedric 2013-4-23
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 2013-4-23
Mm, let me be more clear on this. With my example matrix B, I want an image like:

请先登录,再进行评论。

采纳的回答

Cedric
Cedric 2013-4-23
编辑:Cedric 2013-4-23
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 个评论
Cedric
Cedric 2013-4-23
编辑:Cedric 2013-4-23
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 2013-4-23
编辑:Edgaris Rhomer 2013-4-23
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! :)

请先登录,再进行评论。

更多回答(1 个)

Image Analyst
Image Analyst 2013-4-23
  3 个评论
Image Analyst
Image Analyst 2013-4-23
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 2013-4-23
I'm going to take a look at how to make a movie with MATLAB after I finish this project...! Thanks a lot! :)

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Convert Image Type 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by