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

조회 수: 2 (최근 30일)
Edgaris Rhomer
Edgaris Rhomer 2013년 4월 22일
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에서 Timing and presenting 2D and 3D stimuli에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by