这里有几个例子可以在MATLAB中为动画绘制的方法
修改for循环中的图:
您可以创建一个循环,您可以在其中更改曲面坐标,使用SET命令更新绘图对象,并使用PAUSE命令在短时间内暂停每个循环迭代.以下是一个例子:
[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
当您运行上述操作时,您应该看到椭圆体的长轴收缩直到它是一个球体.
用定时器修改图:
您还可以使用timer object而不是循环来执行情节更新.在这个例子中,我将首先制作一个我想在每次触发定时器时执行的timer_fcn函数:
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
现在我可以创建绘图和计时器并启动计时器如下:
[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-loop示例相同的动画.一旦你完成了定时器对象,记得总是删除它:
delete(animationTimer);