# 3 steps tutorial to quickly create movie 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');
```

## 6 thoughts on “3 steps tutorial to quickly create movie with Matlab”

1. Suhan says:

It was very helpful. Now can you please let me know how to create move in realtime

Like

• Thanks Suhan. There a post on creating movie is matlab on this blog. But I don’t know what you mean by realtime. Thanks for reading.

Like

2. Shafali Gupta says:

Hi, I am stuck in the last step i.e. movie2avi(mov, ‘moviename.avi’, ‘compression’, ‘None’);
It shows this error i.e.
Error using audiovideo.internal.aviwriter (line 184)
Error creating AVI file. Check directory permissions.

Error in movie2avi (line 63)
avimov = audiovideo.internal.aviwriter(filename,varargin{:});