在 Matlab 中绘图“while”环形

发布于 2024-11-30 14:27:20 字数 643 浏览 2 评论 0 原文

为什么我不能在同一窗口中绘制每次迭代的数据?我尝试使用drawnow,但它不起作用。代码:

t=0;
T=10;
i =1;

while t<T
. . .

time(i)=(i-1)*delta_t;

scrsz = get(0,'ScreenSize');

figure('position',[80 60 scrsz(3)-110 scrsz(4)-150]);

subplot(1,3,1);
plot(time(i),configurations(1,1,i),'-b','LineWidth',2), hold on;
drawnow;
xlabel('Time[s]');
ylabel('X [m]');

subplot(1,3,2);
plot(time(i),configurations(3,1,i),'-b','LineWidth',2), hold on;
drawnow;
xlabel('Time[s]');
ylabel('Z [m]');

subplot(1,3,3);
plot(time(i),configurations(2,2,i),'-b','LineWidth',2), hold on;
drawnow;
xlabel('Time[s]');
ylabel('\phi [deg]');

t=t+1;
i=i+1;

end

Why can't I plot data in every iteration in the same window? I tried with drawnow, but it isn't working. Code:

t=0;
T=10;
i =1;

while t<T
. . .

time(i)=(i-1)*delta_t;

scrsz = get(0,'ScreenSize');

figure('position',[80 60 scrsz(3)-110 scrsz(4)-150]);

subplot(1,3,1);
plot(time(i),configurations(1,1,i),'-b','LineWidth',2), hold on;
drawnow;
xlabel('Time[s]');
ylabel('X [m]');

subplot(1,3,2);
plot(time(i),configurations(3,1,i),'-b','LineWidth',2), hold on;
drawnow;
xlabel('Time[s]');
ylabel('Z [m]');

subplot(1,3,3);
plot(time(i),configurations(2,2,i),'-b','LineWidth',2), hold on;
drawnow;
xlabel('Time[s]');
ylabel('\phi [deg]');

t=t+1;
i=i+1;

end

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

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

发布评论

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

评论(1

拥抱我好吗 2024-12-07 14:27:20

这是因为您已在 while 循环内部添加了 figure('...') 行。因此它每次迭代都会打开一个新窗口。移动该行和 scrsz=... 行,并将其放置在 while t 行上方(即,外部循环) 。

要绘制到多个图形窗口,请像这样使用轴句柄:

hFig1=figure(1);hAxes1=axes;
hFig2=figure(2);hAxes2=axes;

while ...
    ---
    plot(hAxes1,...)
    plot(hAxes2,...)
end

但是,每个子图都会创建自己的一个轴。因此,如果您想在循环内的两个不同窗口中绘制多个子图,则必须在循环之前设置它们,然后相应地调用。 IE,

hFig1=figure(1);
hAxes1Sub1=subplot(1,2,1);
hAxes1Sub2=subplot(1,2,2);

hFig2=figure(2);
hAxes2Sub1=subplot(1,2,1);
hAxes2Sub2=subplot(1,2,2);

while ...
    ---
    plot(hAxes1Sub1,...)
    plot(hAxes2Sub1,...)
end

It's because you've added the figure('...') line inside the while loop. So it opens a new window every iteration. Move that line and the scrsz=... line and place it just above the while t<T line (i.e., outside the loop).

To plot to more than one figure window, use axes handles like so:

hFig1=figure(1);hAxes1=axes;
hFig2=figure(2);hAxes2=axes;

while ...
    ---
    plot(hAxes1,...)
    plot(hAxes2,...)
end

However, each subplot creates an axis of its own. So if you want to plot to multiple subplots in two different windows inside the loop, you'll have to set them up before the loop and then call accodringly. i.e.,

hFig1=figure(1);
hAxes1Sub1=subplot(1,2,1);
hAxes1Sub2=subplot(1,2,2);

hFig2=figure(2);
hAxes2Sub1=subplot(1,2,1);
hAxes2Sub2=subplot(1,2,2);

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