views:

408

answers:

3

how to animate a surface if it's coordinates change in time (e.g. ellipsoid) using Matlab?

+5  A: 

Here are a couple of examples of ways you can animate plots in MATLAB...

Modify a plot in a for loop:

You can create a loop in which you change the surface coordinates, update the plot object using the SET command, and use the PAUSE command to pause each loop iteration for a short period of time. Here's an example:

[x,y,z] = ellipsoid(0,0,0,4,1,1);  %# Make an ellipsoid shape
hMesh = mesh(x,y,z);               %# Plot the shape as a mesh
axis equal                         %# Change the axis scaling
for longAxis = 4:-0.1:1
  [x,y,z] = ellipsoid(0,0,0,longAxis,1,1);   %# Make a new ellipsoid
  set(hMesh,'XData',x,'YData',y,'ZData',z);  %# Update the mesh data
  pause(0.25);                               %# Pause for 1/4 second
end

When you run the above, you should see the long axis of the ellipsoid shrink until it is a sphere.


Modify a plot with a timer:

You can also use a timer object instead of a loop to execute the updates to the plot. In this example, I'll first make a function timer_fcn that I want executed each time the timer fires:

function timer_fcn(obj,event,hMesh)
  n = get(obj,'TasksExecuted');               %# The number of times the
                                              %#   timer has fired already
  [x,y,z] = ellipsoid(0,0,0,4-(3*n/40),1,1);  %# Make a new ellipsoid
  set(hMesh,'XData',x,'YData',y,'ZData',z);   %# Update the mesh data
  drawnow;                                    %# Force the display to update
end

Now I can create the plot and timer and start the timer as follows:

[x,y,z] = ellipsoid(0,0,0,4,1,1);  %# Make an ellipsoid shape
hMesh = mesh(x,y,z);               %# Plot the shape as a mesh
axis equal                         %# Change the axis scaling
animationTimer = timer('ExecutionMode','fixedRate',...  %# Fire at a fixed rate
                       'Period',0.25,...                %#   every 0.25 seconds
                       'TasksToExecute',40,...          %#   for 40 times and
                       'TimerFcn',{@timer_fcn,hMesh});  %#   run this function
start(animationTimer);  %# Start timer, which runs on its own until it ends

This will display the same animation as the for-loop example. And once you're done with the timer object, remember to always delete it:

delete(animationTimer);
gnovice
Really good, Only one suggestion. Change your for loop variable from i to k, i and j are usually used for complex numbers.
flopex
While I find the anymate solution is a lot more convenient, his answer is an excellent example to teach about handles and timers at the same time. +1
Jonas
+1  A: 

Are you wanting to have the animation display on the screen or save it as a video file? If you want the animation to display on the screen, you can have your program repeatedly redraw the plot that you are plotting to, with a pause in there, like gnovice has in his answer that just popped up.

If you want to save to a file for replay, I would suggest looking at the movie function (tutorial here) and possibly the helper mpgwrite tool from the MATLAB file exchange.

Michael Herold
I have had success with mpgwrite in the past.
bta
+1  A: 

If you want an easy way to create animations, have a look at ANYMATE from the file exchange. Look at the help to the file and the examples to see how you do an animation in a figure or create animated gifs.

Have a look at the review of anymate in the file exchange pick of the week

EDIT

Here's how you'd animate the ellipsoid from @gnovice's example with anymate

%# create an sphere
[xs,ys,zs] = sphere; %# default is center at 0, radius 1
%# create an ellipsoid
[xe,ye,ze] = ellipsoid(0,0,0,4,1,1);

%# use anymate to interpolate between the two
anymate(@surf,{cat(3,xe,xs) cat(3,ye,ys) cat(3,ze,zs)});
%# color the surface
colormap(jet);
%# fix axes
axis equal

In the figure, there will be a 'movie' toolbar, where you can hit 'play' and watch the animation. Or you can save it to file.

Jonas

related questions