在 MATLAB 中循环内部绘图

发布于 2024-09-01 02:06:26 字数 143 浏览 1 评论 0原文

我正在做这样的事情:

a = [1:100];
for i=1:100,
    plot([1:i], a(1:i));
end

我的问题是在循环完成之前不会显示绘图。 如何在每次迭代中显示/更新绘图?

I am doing something like this:

a = [1:100];
for i=1:100,
    plot([1:i], a(1:i));
end

My issue is that the plot is not shown until the loop is finished.
How can I show/update the plot in every iteration?

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

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

发布评论

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

评论(4

夜清冷一曲。 2024-09-08 02:06:26

使用 DRAWNOW

a = [1:100];
for i=1:100,
 plot([1:i], a(1:i));
 drawnow
end

或者,您可能想看看来自文件交换的 ANYMATE

Use DRAWNOW

a = [1:100];
for i=1:100,
 plot([1:i], a(1:i));
 drawnow
end

Alternatively, you may want to have a look at ANYMATE from the file exchange.

帅哥哥的热头脑 2024-09-08 02:06:26

如果您只想将其可视化而不保存动画,则另一种方法是使用 refreshdata 而不是 plot 进行后续绘图。您仍然需要调用 drawnow 才能使其在屏幕上更新。

无论是使用

set(fig_handle,'XData',new_xdata_array)
set(fig_handle,'YData',new_ydata_array)
refreshdata
drawnow

还是用于

set(fig_handle,'XDataSource',xdata_array)
set(fig_handle,'YDataSource',ydata_array)

%call this whenever xdata_array and ydata_array are assigned new values to see it updated in the plot
refreshdata
drawnow

您的示例,这可能看起来像:

a=[1:100];

figure;
h=plot(1,a(1));
for i=2:100
  set(h,'XData',[1:i])
  set(h,'YData',a(1:i))
  refreshdata
  drawnow
end

对于简单的线图来说并不是那么有用(对于其中 plot();drawow; 更简单更快),但是当您需要创建涉及多种绘图类型的更复杂的图形,这可能很有用。

Another way to do this if you just want to visualise it without saving the animation, is to use refreshdata instead of plot for subsequent plots. You will still need to call drawnow for it to update on-screen.

either use

set(fig_handle,'XData',new_xdata_array)
set(fig_handle,'YData',new_ydata_array)
refreshdata
drawnow

or use

set(fig_handle,'XDataSource',xdata_array)
set(fig_handle,'YDataSource',ydata_array)

%call this whenever xdata_array and ydata_array are assigned new values to see it updated in the plot
refreshdata
drawnow

for your example, this might look like:

a=[1:100];

figure;
h=plot(1,a(1));
for i=2:100
  set(h,'XData',[1:i])
  set(h,'YData',a(1:i))
  refreshdata
  drawnow
end

It's not all that useful for simple line plots (for which plot(); drawnow; is simpler and faster), but when you need to create more complicated figures involving multiple plot types, this can be useful.

蹲墙角沉默 2024-09-08 02:06:26

来自 comet.m 文档

t = 0:.01:2*pi;
x = cos(2*t).*(cos(t).^2);
y = sin(2*t).*(sin(t).^2);
comet(x,y);

From the documentation for comet.m

t = 0:.01:2*pi;
x = cos(2*t).*(cos(t).^2);
y = sin(2*t).*(sin(t).^2);
comet(x,y);
萌梦深 2024-09-08 02:06:26

Matlab 允许您对变量的循环语句进行排序自动化

x = 0.0:0.1:2*pi

plot(x,cos(x));

就是一个例子……

很多时候您并不需要在循环中进行绘制

Matlab allows you to sort-of automate a loop statement for variables

x = 0.0:0.1:2*pi

plot(x,cos(x));

is an example......

A lot of times you don't really need to plot 'in' a loop

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