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.
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.
Allocate the movie structure that hold info from the frames. We call it mov.
Now the real capturing of frames. we will plot 10 data columns of x and get them into a movie.
We use a do loop to capture the frame.
for k=1:nframe plot(x(:,k)) mov(k)=getframe(gcf); end
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.
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');