Animated Plots
(MATLAB Movies)
· Animations are formed by a
sequence of plot statements (each with a slightly different curve). The command getframe is used
to capture each plot and store it. The movie( ) command plays back the frames as a movie.
>> help getframe
GETFRAME
Get movie frame.
GETFRAME returns a movie
frame. The frame is a snapshot
of the current axis. GETFRAME
is usually used in a FOR loop
to
assemble an array of movie frames for playback using MOVIE.
For example:
for j=1:n
< plot_commands goes here >
M(j)
= getframe;
end
movie(M)
· M is a special type of array,
which holds all the frames. Note that
like any array index, j must be an integer or
the command M(j)=getframe; would produce
an error.
· Copy and Paste the following
code into an m-file (projectile.m)
% Script file (projectile.m) to plot an animation of a projectile
trajectory
%
% Author…
% Date…
clear; close all; clc
% Get the constants we need
angd=input('What angle do
you want (degrees):');
v0=input('What is the
initial velocity (ft/sec):');
numpts=input('How many points
to evaluate (I suggest 50):');
% Run our trajecrtory
function
[x,y,t]=trajectory(angd,v0,numpts);
% display results as animation
figure
for n=1:length(x)
plot(x(n),y(n),'ro');
axis equal;
axis([0,max(x),0,max(y)+10]);
xlabel('x (ft)');
ylabel('y (ft)');
title('Projectile
Trajectory');
M(n)=getframe;
end
% play as smooth movie 3 times at 10
frames per second
% note that it goes through the frames
2x initially to get ready for
% full speed
play. So it will actually
play 2x slower and 3x at full speed.
numtimes=3;
fps=10;
movie(M,numtimes,fps)
· Execute the command by
typing >> projectile and enjoy…
· Let’s plot a sinusoid with
the frequency changing before our eyes.
Copy the following code into an m-file (sinmovie.m).
% Script file (sinmovie.m) to plot an animation of a sin function with
increasing
% frequency.
%
% Author…
% Date…
clear;close all;clc
t=linspace(0,2*pi,1000);
count=1;
figure
for freqrps=.1:.1:2*pi
y=sin(freqrps*t);
plot(t,y);
xlabel('x');
ylabel('y');
axis([0,2*pi,-1,1])
S1=sprintf('y(t)=sin(%.2f t)',freqrps);
text(2,.6,S1)
freqcps=freqrps/(2*pi);
S2=sprintf('frequency=%.2f rads/sec (%.2f cyc/sec)',freqrps,freqcps);
text(2,.4,S2)
title('Sinusoidal
Function');
M(count)=getframe;
count=count+1;
end
movie(M,2,10);
·
Execute the command by typing >> sinmovie and enjoy…
Saving MATLAB as . avi’s
·
Let’s say you want to save your movie in a file format that can be
played on just about any PC (without MATLAB installed) or you want to include
your movie on a web page or in a Power Point Presentation.
· You can save your movie
(usually M) as an .avi
file (see below) using the command movie2avi( ).
· AVI (.avi) Audio
Video Interleave – Microsoft AVI was developed by Microsoft to add Multi-Media
capabilities to Windows. It is the most common Multi-Media format if for no
other reason than it is included on every PC running any version of Windows. It
is capable of combining both Audio and Video into a relatively high quality
file.
· Here is an
example using your sin function movie we created above:
movie2avi(M, 'sinusoid.avi');
· You may get a few
warnings, that’s typical. It will still work. Now use ls to verify that
your .avi file is in your current directly. Use pwd to see the full
path. Outside of MATLAB, work you way down to this directory and double click on your new
file. It should play using windows media
player (independent from MATLAB).
· To avoid codec
issues you may wish to save with no compression
movie2avi(M, 'sinusoid.avi', 'compression', 'none');
·
Sometimes the axes will not appear correctly in
the .avi file.
To fix this, use a figure handle with the getframe
command like so:
clear;close all;clc
t=linspace(0,2*pi,1000);
count=1;
h = figure(1);
set(h,'Position',[100
678 560 420])
for freqrps=.1:.1:2*pi
y=sin(freqrps*t);
plot(t,y);
xlabel('x');
ylabel('y');
axis([0,2*pi,-1,1])
S1=sprintf('y(t)=sin(%.2f t)',freqrps);
text(2,.6,S1)
freqcps=freqrps/(2*pi);
S2=sprintf('frequency=%.2f rads/sec (%.2f cyc/sec)',freqrps,freqcps);
text(2,.4,S2)
title('Sinusoidal
Function');
M(count)=getframe(h);
count=count+1;
end
movie2avi(M, 'sinusoid.avi', 'compression', 'none');