MATLAB 中的实时绘图

发布于 2024-09-07 03:47:19 字数 395 浏览 1 评论 0原文

我对 MATLAB 非常陌生,我试图显示一些计算的实时图。我有一个 N 大小的向量,并且一次使用 m 个值(例如 m = N/4),所以我想绘制第一个 m 值,然后一旦计算出第二个 m 值,就用它们替换第一个图。

我的方法如下:

for i=1:N,
  ...
  //compute m
  ...
  plot(m);
end;

但它无法在每个循环中更新绘图,并等待所有循环完成才能绘制数据。我的问题是:我应该使用另一个函数而不是plot,还是可以在每个循环中添加一些延迟?

我认为一定有一种我不知道的方法来更新绘图而不是每次都重新绘制它。

I'm very new to MATLAB and I was trying to display a real time plot of some calculations. I have an N sized vector and I work with m values at a time (say m = N/4), so I want to plot the first m values and then as soon as the second m values are calculated have them replace the first plot.

My approach was as follows:

for i=1:N,
  ...
  //compute m
  ...
  plot(m);
end;

but it fails to update the plot in every loop and waits for all the loops to finish to plot the data. My question is: Should I use another function instead of plot or could I add some delay in each loop?

I think there must be a way I'm not aware of for updating the plot instead of re-plotting it every time.

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

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

发布评论

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

评论(2

一曲爱恨情仇 2024-09-14 03:47:19

正如Edric提到的,你肯定会想要包含一个drawnow 命令在调用 plot 强制更新图形。然而,有一种更有效、更流畅的方法来动画绘图,而无需每次都重新创建整个绘图。您可以简单地初始化绘图,捕获句柄 到绘图对象,然后使用 < 修改循环中该对象的属性代码>设置命令。这是一个示例:

hLine = plot(nan);         % Initialize a plot line (which isn't displayed yet
                           %   because the values are NaN)
for i = 1:N                % Loop N times
  ...
  % Compute m here
  ...
  set(hLine, 'YData', m);  % Update the y data of the line
  drawnow                  % Force the graphics to update immediately
end

此外,在循环之前和调用 plot 之后,您可以设置多个 坐标轴属性,如 轴限制等,如果您希望轴保持固定并且不随着每个新向量 m< 改变其外观/code> 已绘制。

As Edric mentioned, you'll definitely want to include a drawnow command after the call to plot to force an update of the graphics. However, there is a much more efficient and smoother method to animate plots that doesn't involve recreating the entire plot each time. You can simply initialize your plot, capture a handle to the plot object, then modify the properties of that object in your loop using the set command. Here's an example:

hLine = plot(nan);         % Initialize a plot line (which isn't displayed yet
                           %   because the values are NaN)
for i = 1:N                % Loop N times
  ...
  % Compute m here
  ...
  set(hLine, 'YData', m);  % Update the y data of the line
  drawnow                  % Force the graphics to update immediately
end

In addition, before your loop and after the call to plot you can set a number of axes properties, like the axes limits, etc., if you want the axes to stay fixed and not change their appearance with each new vector m that is plotted.

虚拟世界 2024-09-14 03:47:19

您可以添加对 DRAWNOW 的调用以强制更新绘图。请参阅参考页面。请注意,DRAWNOW 会导致图形事件队列被刷新,这可能会导致执行回调等。

You can add a call to DRAWNOW to force the plot to update. See the reference page. Note that DRAWNOW causes the graphics event queue to be flushed, which may cause callbacks etc. to be executed.

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