How do I animate a surface if it's coordinates change in time (e.g. ellipsoid) using M开发者_开发知识库ATLAB?
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);
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.
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.
One little difference I wanted outline between the 2 implementations given above:
1) pause():
pause() can be used for simple animations with little data. It is my preferred method since it is simple and straightforward. But I only use pause if the animation requires little data, since pause() blocks processing for the ammount of time given.
2) Timer: If I want to animate spectrograms or spectra and calculate them in "real-time" or sync them with audio I usually use timer object, which don't block processing in the meanwhile. If I use pause() with such animations, sync between audio and animation is lost quickly...
精彩评论