Creating movie from Images from a for loop

237 views (last 30 days)
Hi there, I am currently producing images from a for loop, however I would like put these images into a movie or slideshow,however I am struggling to do this. Any help would be appreciated.
if true
% code
end
for i=t:Images
figure(i)
B=readimx(fullfile(filename,['B000',int2str(i),'.im7']));
C=B.Frames{1}.Components{1};
V = C.Planes;
I2=V{1,1};
Array3D(:,:,i-t+1)=I2;
K=imagesc(flipud(Array3D(:,:,i-t+1)));
set(gca,'YDir','normal');
end

Accepted Answer

Oliver Woodford
Oliver Woodford on 29 May 2015
Produce your images programmatically, rather than by exporting a figure, if you can, as it will be much faster. SC (on the FEX) is good for this. Editing Joseph Cheng's code, you would do the following:
[y, x] = ndgrid(1:256);
vidfile = VideoWriter('testmovie.mp4','MPEG-4');
open(vidfile);
for ind = 1:256
z = sin(x*2*pi/ind)+cos(y*2*pi/ind);
im = sc(z, 'hot');
writeVideo(vidfile, im);
end
close(vidfile)
  1 Comment
Marcus Lehr
Marcus Lehr on 24 Aug 2018
Hi Oliver,
I'm trying to use this method however the video file I'm trying to make is of contour plots. writeVideo() gives me the following error:
IMG must be of one of the following classes: double, single, uint8
I've tried converting the data with C2xyz and saving it with SC but it hasn't helped. sc() returns
Conversion to double from cell is not possible.
Any idea how I can convert data created by contour() into a normal image file that I can write? The only workaround I've so far is saving each plot with saveas(), then converting the resulting image files to a stack with imagej. Any better solutions would be appreciated.
Thanks

Sign in to comment.

More Answers (1)

Joseph Cheng
Joseph Cheng on 29 May 2015
Edited: Joseph Cheng on 29 May 2015
you can follow the example of getframe() in the documentation located here:
example:
x=1:256;
[x y] = meshgrid(x,x);
figure(1)
vidfile = VideoWriter('testmovie.mp4','MPEG-4');
open(vidfile);
for ind = 1:256
z=sin(x*2*pi/ind)+cos(y*2*pi/ind);
imagesc(z),colormap(hot)
drawnow
F(ind) = getframe(gcf);
writeVideo(vidfile,F(ind));
end
close(vidfile)

Categories

Find more on Convert Image Type 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!