Thursday, 9 July 2009

Making a movie (avi) with MATLAB

Movies are a very useful tool in order to visualise the evolution of a process with time. It's very easy to make movies with MATLAB. Here, we are going to demonstrate 100 random points moving in time. First we open an aviobj which contains the name of the movie. We can define the frames per second for the movie. Under Linux, compression is not possible. However, you can define the quality and compression method on other platforms. The rest of the script simply defines the points and at each iteration adds a random jitter to each point. Every time plot is called, that frame is added to the aviobj. Once aviobj is closed, the movie is saved. Use your favourite editor to view the movie.

fig = figure;

aviobj = avifile('testmovie.avi','compression','None');

aviobj.fps = 20;

xcoords = 100.*rand(1,100);

ycoords = 100.*rand(1,100);

for i = 1 : 100

xcoords = xcoords + (-2 + 4.*rand(1,100));

ycoords = ycoords + (-2 + 4.*rand(1,100));

plot(xcoords,ycoords,'b*','markersize',10); hold on;

axis([0 100 0 100]); hold off;

F = getframe(fig);aviobj = addframe(aviobj,F);

end

close(fig)

aviobj = close(aviobj);

Since the file was 68 MB, I uploaded it to YouTube. Here it is:

No comments:

Post a Comment