% A Short Introduction to Movie Making in Matlab
% Lacra Bintu
% Matlab stores movies as information in matrices.
% The information for each frame is stored as to one column in the movie matrix.
% Somewhere near the beginning of your file you must initialize your movie matrix.
% Suppose you want to name your movie MyFilm, and you want it to have N frames.
N=100; % say how many frames you want
MyFilm=moviein(N); % initialize the movie matrix;
for i=1:N
x=cos(2*pi*i/N); y=sin(2*pi*i/N); % The simplest example I could think of:
% a point moving in a circle
figure(1)
plot(x,y, 'o')
axis([-1.5 1.5 -1.5 1.5]) % fixing the axis, so that they will
% be the same for all frames
% axis([XMIN XMAX YMIN YMAX])
MyFilm(:,i)=getframe; % takes the information from the current picture
% and puts it in the ith column of the movie matrix
% (this will be the ith frame of MyFilm)
end % It's a wrap! That's all there is to it.
movie(MyFilm) % Plays Myfilm
% Say you want to save the movie
% in a file called MyBest (you don't need to):
Save MyFilm MyBest
% To play the saved movie:
load MyBest % This loads the information stored in the movie file
Movie(MyFilm) % This plays the movie that had been stored in
% MyBest (meaning the movie MyFilm)
% So to recapitulate:
% = moviein(); % moviein initializes the movie matrix
% (:,i) % saves contents of current figure in
% i-th column of the movie matrix.
% Save % Saves movie matrix in a data file%
% load % Reads movie data stored in
% Movie % Plays as an animation
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Sometimes, when you are drawing more than one object
% in each frame you might want to use hold on and hold off.
% If you put hold on after plotting the first object,
% when you plot another object (in the same frame)
% the first object does not disappear from view.
% If you put hold off after the second plot, the next
% plot after that will cover everything already in your figure.
% Below is a quick example, using the same code as before,
% but now making use of hold on and hold off.
% (If you didn't understand what I just said above about hold
% on and hold off, ust try experimenting
% with the code below, by deleting first
% hold off and then hold on and see what happens.
% That should make it it clear.)
N=100; MyFilm=moviein(N);
for i=1:N
x=cos(2*pi*i/N); y=sin(2*pi*i/N);
figure(1)
plot(x,y, 'o')
hold on
x1=[0 x]; y1=[0 y];
plot(x1,y1) %draws the radius of the circle
hold off
axis([-1.5 1.5 -1.5 1.5])
MyFilm(:,i)=getframe;
end
movie(MyFilm)
% If you use an earlier version of Matlab,
% you can turn your movie into an avi file
% using movie2avi.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Here is a more sophisticated and interesting example.
% It is the contents of a script M-File called ellipse.m
% This file creates and plays a movie showing the osculating circles to an ellipse
% Lacra Bintu
clear all
close all
n=100; % the number of frames
Mellipse=moviein(n); % initializing the matrix that will contain the information for the movie
a=1.6; % the value of the big axis of the ellipse
b=0.9; % the value of the big axis of the ellipse
t=[0:2*pi/n:2*pi]; % setting the values of t where we want to see the circles
x=a*cos(t); % finding the x coordinate from the parametric equation of an ellipse
y=b*sin(t); % finding the y coordinate from the parametric equation of an ellipse
k=a*b./(a^2.*sin(t).^2+b^2*cos(t).^2).^(3/2); % the curvature of the ellipse at the chosen t values
r=1./k; % the radii of the osculating circles at each chosen value of t
xo=x-b*r.*cos(t)./((b^2)*cos(t).^2+a^2*sin(t).^2).^(1/2); % the x cordinate of the centers of the circles
yo=y-a*r.*sin(t)./((b^2)*cos(t).^2+a^2*sin(t).^2).^(1/2); % the y cordinate of the centers of the circles
m=2*max(r)-min([a,b]); % calculating about how big we want the axes of our figures to be
for i=1:n+1
xc=xo(i)+r(i)*cos(t); % finding all the x coordinates necessary to draw the osculating
% circle at the point i*2pi/n
yc=yo(i)+r(i)*sin(t); % finding all the x coordinates necessary to draw the osculating
% circle at the point i*2pi/n
figure(1)
plot(x,y, 'k') % draw the ellipse
hold on % keep the figure from being erased
plot(xc,yc,'r') % draw the osculating circle at at the point i*2pi/n
plot(xo(1:i),yo(1:i)) % draw all the centers of the circles up to that point
plot([xo(i),x(i)], [yo(i),y(i)], 'r--') % draw the normal to the ellipse
hold off % allow the figure to be erased
axis([-m m -m m]) % set the axis to some appropriate value
Mellipse(:,i)=getframe; % put the information from the resulting figure
% as a column in the movie matrix
end
movie(Mellipse) % play the movie