Making animations & movies with Matlab
getframe
& movie2avi
Animation or movie: a succession of pictures
Drawing plots and pictures is rather simple with Matlab; that's a main feature of Matlab.Creating a movie is thus just as simple, simply by creating succession of pictures.
For example,
clear all;close all;clc % Clean all before start
tmax=13;
figure(1);clf;
hold on, % keeping each plot
view(3) % 3D view
J=jet; % jet is a matrix containing common colors (64)
for k=0:0.1:tmax
t=k:0.01:k+1;
z=exp(-0.2*t+2*1i*pi*t);
c=round(63/tmax*k)+1;
% J(c,:) is [R G B] for the color number c in J=jet
% very plot instructions
plot3(t,real(z),imag(z),'linewidth',3,'color',J(c,:))
axis([0 10 -1 1 -1 1]);axis off
pause(0.05) % A brief pause...
end
yielding the following result:
Makign out a movie
getframe
enable to capture a figure as an image.
Creating a movie consists in the concatenation of getframed pictures in a vector. The movie can then be played and watched using
movie
command.
More interessingly, the movie created can be recorded, into an
avi
movie, with movietoavi
(see help movie2avi
for all arguments and parameters: use
of codecs, compression level, number of frame per second, …)
clear all;close all;clc % Clean before start
tmax=13;
figure(1);clf;
hold on, % Holding down each plot
view(3) % 3D view
J=jet; % Color map
GF=[];
for k=0:0.1:tmax
t=k:0.01:k+1;
z=exp(-0.2*t+2*1i*pi*t);
c=round(63/tmax*k)+1;
% Very plot instruction
plot3(t,real(z),imag(z),'linewidth',3,'color',J(c,:))
axis([0 10 -1 1 -1 1]);axis off
GF=[GF getframe]; % getframes are concatened inside vector GF
% pause(0.05) the brief pause is now useless...
end
movie2avi(GF,'film.avi');