Monday, April 19, 2010

Making movies in MATLAB ( for simulation purposes)

o make a movie of MATLAB figures first have MATLAB generate the frames of the
movie. An example file is the following…
% initialize vectors (x and y) and the axes used for plotting
nx=40;
x=linspace(-2,2,nx);
y=exp(-8*x.*x);
plot(x,y);
lim=axis;
% nframes = number of frames in movie
nframes=40;
M=moviein(nframes);
% Note: memory can be reduced by making the plot window smaller
% and commands to get and set the current plot size are…
%get(gca,’Position’)
%set(gca,’Position’,[0.13 0.11 0.5 0.6]);
% loop to produce frames of the movie (frames stored in matrix M)
dt=2/nframes;
for it=1:nframes
z=x-sin(2*pi*it*dt);
y=exp(-8*z.*z);
plot(x,y)
axis(lim)
drawnow;
M(:,it)=getframe;
end;
% various commands to show movie
movie(M);
movie(M,3);

B. Turning a MATLAB Movie into an MPEG File
You can turn the movie into a MPEG file by using the mpgwrite command
map=colormap;
mpgwrite(M, map,’filename’)
where ‘filename’ is the string containing the name of the MPEG file. Note this
is not a regular MATLAB command and the source file for it can be found at
http://www.mathworks.com/ftp/graphicsv5.shtml. Alternatively, you can copy the
files in the directory ~holmes/public/mpgwrite into the directory where you
keep m-files on RCS (note this will only work on UNIX machines). To play these
movies on RCS you can use a command like the following…
mpeg_play filename.mpg
or
mpeg_play -loop filename.mpg

TIDBITS:

While running the code make sure the plot is visible (as getframe captures the plot) and dont run other applications keepping matlab minimized. Then u will get only a blank movie.

While converting it to mpeg file make sure that the structures in M have the same frame values (exactly not sure of the tech term…. jus open each struct available in the array M nd see if the value it stores are same in case u are not able to convert it)

Link for downloading mpgwrite http://www.mathworks.com/matlabcentral/fileexchange/309

Any querries pls comment…

No comments: