Saturday, September 28, 2013

How to Generate Animation Video using Matlab?

Let's say you have a mathematical function changing in time (or in any other quantity) and you would like to watch it progress and create an animation out of it. That is very simple to integrate into an existing Matlab code. Basically, by using the mov and getframe(), addframe() commands, one can easily generate the animation file. Below is a sample code snippet for further reference:


mov = avifile('test.avi','fps',7,'quality',100,'keyframe',1);
     
     
    figure('Renderer','zbuffer')
    Z = peaks;
    surf(Z);
    axis tight
    set(gca,'NextPlot','replaceChildren');
    % Preallocate the struct array for the struct returned by getframe
    F(20) = struct('cdata',[],'colormap',[]);
    % Record the movie
    for j = 1:200
        surf(.01+sin(2*pi*j/20)*Z,Z)
        F(j) = getframe;
     
        F = getframe(gcf);
        mov = addframe(mov,F);
     
        pause(0.1);
    end
     
    mov = close(mov); 





Note: I used the following website to obtain the highlighted Matlab code in html: http://tohtml.com/matlab/

No comments:

Post a Comment