Create Movie in Matlab


You spent a long time extracting statistics from data and creating meaningful figures with appropriate line-styles and markers. Now you want to create a video by just concatenating these figures. Some lines of matlab code and you are done.

Just spent too much time to create a movie in matlab. This script takes 10 figures which are already displayed, resizes them to full screen resolution (all figures must have the same resolution) and captures the screen to get a matlab movie. This matlab movie is then converted to an avi.


scrsz = get(0,'ScreenSize');
for i = 1:10
fig=figure(i);
set(fig,'Position',scrsz);
M(i)=getframe(fig);
end
movie2avi(M,'filename.avi', 'compression', 'none');


You may want to substitute filename for something more telling and make number of figures appropriate for your purpose.

Without compression these files are very big. In order to send them to other people by email you might want to compress them. For compression you might have to download codecs or you use a compression utility such as mencoder afterwards:

> mencoder filename.avi -oac mp3lame -ovc lavc -lavcopts acodec=mp3,1bitrate=128,vcodec=mpeg4,vbitrate=800,vhq,vm4v -o filename_s.aviEdit

If you want to view these movie files, you can use the VLC Media Player, where you activate view->advanced controls and then you have button (rightmost new button) for viewing frame by frame.

Here comes a short function for exporting videos:

function make_video(basename,outfile)
% loads all files that match basename (use wildcards like '*' if you like)
% and write them to a video file.
% The file can be quite big. I recommend you compress it.
% Example:
% make_video('orn_network*.bmp','orn_network.avi');
a=dir(basename);
files = sort_nat({a.name});
scrsz = get(0,'ScreenSize');
for i = 1:numel(files)
fig=figure;A=imread(files{i});image(A);
set(fig,'Position',scrsz);
M(i)=getframe(fig);
close;
end
movie2avi(M,outfile, 'compression', 'none');


Enjoy. Please leave questions and suggestions in the comment section. 

You might also be interested in my article on exporting figures from matlab. I also wrote articles about exporting data from matlab and how to get good combinations of linestyles and colors for plots.

5 Responses to "Create Movie in Matlab"

thank you for the script!I used to spend a lot of time with matlab...

That looks very tricky - I'm not good at using script. I might stick with my Windows Movie Maker for now ;)

Thank you for this script it should definitely save me some time the next time I want to create a movie in matlab. Also thanks for the tips for compressing the files by downloading codecs or using mencoder.

many thanks

you don not have to wrrite these two statment
fig=figure(i);set(fig,'Position',scrsz);
the program can be
img = dir('your path\*.bmp');



for i = 1:numel(img)

im =imread(img(i).name);

M(i) = im2frame(im);end
im =imread(img(i).name);





M(i) = im2frame(im);
end



movie2avi (M,'im.avi', 'compression', 'none');
movie2avi (M,'im.avi', 'compression', 'none');


implay(M); thanks
implay(M); thanks

Thanks for the Article, Helped me in my thesis. :)

  Subscribe to replies to this post

 
This conversation is missing your voice. Your feedback is appreciated.
Post a Comment


You can use some HTML tags, such as <b>, <i>, <a>

If you see a message that says "your request could not be processed" press preview first and then post.
 
You can follow the discussion of this post by subscribing.


 
You are free to include information from this article on your own site if you provide a backlink. You can use the following markup:
<a href="http://www.myoutsourcedbrain.com/2010/12/create-movie-in-matlab.html">Create Movie in Matlab</a>