MATLAB 中的动画
如果使用 MATLAB 坐标随时间变化(例如椭球体),如何为表面制作动画?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
如果使用 MATLAB 坐标随时间变化(例如椭球体),如何为表面制作动画?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(4)
以下是在 MATLAB 中对绘图进行动画处理的几个示例...
在 for 循环中修改绘图:
您可以创建一个循环,在其中更改曲面坐标,使用
set
命令,并使用pause
命令将每个循环迭代暂停一小段时间。这是一个例子:当你运行上面的代码时,你应该看到椭球体的长轴收缩,直到它变成一个球体。
使用计时器修改绘图:
您还可以使用 计时器对象 而不是循环来执行绘图更新。在此示例中,我将首先创建一个要在每次计时器触发时执行的函数
timer_fcn
:现在我可以创建绘图和计时器并启动计时器,如下所示:
这将显示相同的动画作为 for 循环的例子。一旦您完成了计时器对象,请记住始终将其删除:
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 thepause
command to pause each loop iteration for a short period of time. Here's an example: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:Now I can create the plot and timer and start the timer as follows:
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:
您想让动画显示在屏幕上还是将其保存为视频文件?如果你想让动画显示在屏幕上,你可以让你的程序重复重绘你正在绘制的图,并在那里暂停,就像 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.如果您想要一种简单的方法来创建动画,请查看 ANYMATE文件交换。查看文件的帮助和示例,了解如何在图形中制作动画或创建动画 GIF。
看看本周文件交换精选中对anymate的评论< /a>
编辑
以下是如何为来自 @gnovice 的椭球体设置动画's example with anymate
在图中,将会有一个“电影”工具栏,您可以在其中点击“播放”并观看动画。或者您可以将其保存到文件中。
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
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.
我想要概述上面给出的两种实现之间的一个小区别:
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...