如何使用MATLAB对随时间变化的曲面(例如椭球体)进行动画处理?
以下是一些在MATLAB中动画绘图的示例...
您可以创建一个循环,在其中更改曲面坐标,使用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循环示例相同的动画。一旦您完成计时器对象的使用,请记得始终删除它:
delete(animationTimer);
请查看文件交换每周精选中关于 AnyMate 的评价 file exchange pick of the week
编辑
以下是使用 anymate 制作@gnovice示例中的椭球体动画的方法。
%# 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
我想要强调一下上述两种实现之间的一个小差别:
1) pause():
pause() 可以用于简单的动画,数据量较小。由于它简单明了,是我首选的方法。但是,如果动画需要大量数据,则只有在给定时间内暂停处理时才使用 pause()。
2) Timer:
如果我想要以“实时”方式或与音频同步地播放光谱图或频谱图像,通常会使用计时器对象,这样可以在同时不阻塞处理。如果我在此类动画中使用 pause(),则音频和动画之间的同步很快就会丢失...