Dec 3, 2010

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.

8 comments:

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

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

    ReplyDelete
  3. 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.

    ReplyDelete
  4. 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

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

    ReplyDelete