from three_dimension_animation.m by Eve Klopf
Code snippet discussing how to make a short movie using simulation results.

three_dimension_animation.m
% three_dimension_animation.m

% This file makes a simple animation out of a number of three-dimensional
% plots.  It's assumed that you've already got the .mat file, probably
% created using some kind of calculation in a loop.  For example, if you're
% performing an FDTD simulation of a wave going through an area of space,
% you can take a snapshot of the simulation results as the plane wave
% travels through different areas of space.  

% A simple way to get the data in a useful format in matlab might go 
% something like this: 
% 1. Plot a slice of your data after the end of the loop containing your
% fdtd field calculation (or whatever you're calculating).  Make sure that
% the plots all have the same axis.:
% [X,Y] = meshgrid(1:IE, 1:JE); 
% surf(X,Y,ez(:,:,ic))
% axis([0 60 0 60 -0.5 1])

% 2. Place the loop containing your fdtd field calculation within another
% loop, which varies the number of iterations (distance traveled by your
% field) that the fdtd simulation runs.  Then, save the plots in a matrix 
% as you iterate along.  You can do this by placing this command directly
% after the axis command for your plots:
% M(q) = getframe;
% Make sure that you leave the Figure visible when you run your simulation.
% For some reason, "getframe" doesn't properly record the picture of your
% simulation if you can't see the figure on your screen.

% 3. Then, outside of both loops, store the M matrix into a .mat file using
% the command:
% save your_file_name.mat M;

% You now have a .mat file containing snapshots of your simulation results
% that can be run as a movie.  You run this movie using a very short
% program placed in the same folder as your .mat file.  The following code
% snippet runs the movie contained in a .mat file called
% "mem_store_twoD_cyl.mat" 3 times in a row.

load mem_store_twoD_cyl.mat;
movie(M,3)

Contact us