MATLAB中的动画

Kat*_*ate 13 matlab plot animation geometry-surface

如果曲面的坐标随时间变化(例如椭圆体),如何使用MATLAB为曲面设置动画?

gno*_*ice 15

以下是几个可以在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
Run Code Online (Sandbox Code Playgroud)

当您运行上述操作时,您应该看到椭球的长轴收缩,直到它是一个球体.


使用计时器修改绘图:

您还可以使用计时器对象而不是循环来执行对绘图的更新.在这个例子中,我将首先创建一个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
Run Code Online (Sandbox Code Playgroud)

现在我可以创建绘图和计时器并启动计时器,如下所示:

[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
Run Code Online (Sandbox Code Playgroud)

这将显示与for循环示例相同的动画.一旦你完成了计时器对象,记得永远删除它:

delete(animationTimer);
Run Code Online (Sandbox Code Playgroud)

  • 真的很好,只有一个建议.将for循环变量从i更改为k,i和j通常用于复数. (2认同)