3 steps tutorial to quickly create movie with Matlab

Animation with matlab

Matlab is ubiquitous in today’s field of engineering . The quick prototyping and convenience of plotting has made it a must tool for engineers.

And many a time, we need to create animation animation or video visualization demos of a simulation, Matlab comes very handy… and with three lines we can get a movie of the simulation.

Here’s a quick 3 step tutorial to get an avi file video of your simulation using Matlab.

But before that lets generate some random data to display and capture as a movie.

x=rand(100,10);

Created x random data with 10 columns.

Movie consists of frames and this are stored in a specific format. So let’s define how many frame we want.

nframe=10;

Step 1:

Allocate the movie structure that hold info from the frames. We call it mov.

mov(1:nframe)=struct('cdata',[],'colormap',[]);

Now the real capturing of frames. we will plot 10 data columns of x and get them into a movie.

Step 2:

We use a do loop to capture the frame.

for k=1:nframe
plot(x(:,k))
mov(k)=getframe(gcf);
end

Step 3:

This done. Now we have all figures in mov structure, lets create the movie

movie2avi(mov, 'moviename.avi', 'compression', 'None');

Done. You should have an AVI file in your current working dir.

But if you look at the video… It feels like flickering. That’s easy to correct just add this before the for loop to prevent flickering.

set(gca,'nextplot','replacechildren') 

That done.

Here’s the full Matlab script

nframe=10;
x=rand(100,nframe);
mov(1:nframe)= struct('cdata',[],'colormap',[]);
set(gca,'nextplot','replacechildren')
for k=1:nframe
  plot(x(:,k))
  mov(k)=getframe(gcf);
end
movie2avi(mov, '1moviename.avi', 'compression', 'None');
Advertisements

4 thoughts on “3 steps tutorial to quickly create movie with Matlab

  1. Pingback: GIF animation in Matlab in 3 steps « SukhbinderSingh.com

  2. Pingback: A year of posts | SukhbinderSingh.com

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s