MATLAB 中的动画

发布于 2024-08-30 07:45:50 字数 44 浏览 8 评论 0 原文

如果使用 MATLAB 坐标随时间变化(例如椭球体),如何为表面制作动画?

How do I animate a surface if it's coordinates change in time (e.g. ellipsoid) using MATLAB?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

双手揣兜 2024-09-06 07:45:50

以下是在 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_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);

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);
允世 2024-09-06 07:45:50

您想让动画显示在屏幕上还是将其保存为视频文件?如果你想让动画显示在屏幕上,你可以让你的程序重复重绘你正在绘制的图,并在那里暂停,就像 gnovice 在他刚刚弹出的答案中所做的那样。

如果您想保存到文件以供重播,我建议您查看 movie 函数(教程 此处)以及可能的帮助器 mpgwrite 工具。

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.

遗失的美好 2024-09-06 07:45:50

如果您想要一种简单的方法来创建动画,请查看 ANYMATE文件交换。查看文件的帮助和示例,了解如何在图形中制作动画或创建动画 GIF。

看看本周文件交换精选中对anymate的评论< /a>

编辑

以下是如何为来自 @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

在图中,将会有一个“电影”工具栏,您可以在其中点击“播放”并观看动画。或者您可以将其保存到文件中。

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.

腻橙味 2024-09-06 07:45:50

我想要概述上面给出的两种实现之间的一个小区别:

1)pause():

pause()可用于数据很少的简单动画。
这是我的首选方法,因为它简单明了。但我只在动画需要很少数据时才使用暂停,因为暂停()会在给定的时间内阻止处理。

2)定时器:
如果我想要制作频谱图或频谱的动画并“实时”计算它们或将它们与音频同步,我通常使用计时器对象,它同时不会阻止处理。
如果我对此类动画使用暂停(),音频和动画之间的同步很快就会丢失......

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...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文