问题描述
我如何动画的表面,如果它在时间坐标的变化(例如椭球)利用Matlab?
How do I animate a surface if it's coordinates change in time (e.g. ellipsoid) using Matlab?
推荐答案
下面是几个方法可以在动画MATLAB地块的例子...
Here are a couple of examples of ways you can animate plots in MATLAB...
您可以创建在其中改变表面坐标循环使用的命令,并使用命令暂停每次循环迭代的时间短的时间。这里有一个例子:
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.
结果
您也可以使用,而不是循环执行更新的情节。在这个例子中,我首先做一个函数 timer_fcn
,我想每次定时器触发执行:
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
这将显示相同的动画作为for循环的例子。而一旦你与定时器对象来完成,记得要经常删除:
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);
这篇关于动画在Matlab的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!