【问题标题】:Plotting in Matlab 'while' loop在 Matlab 'while' 循环中绘图
【发布时间】:2011-08-22 18:05:20
【问题描述】:

为什么我不能在同一个窗口中的每次迭代中绘制数据?我尝试使用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

【问题讨论】:

  • 请定义“不工作”。
  • 每次迭代都会在新窗口中绘制数据。最后我有 10 个绘图窗口。

标签: matlab loops plot


【解决方案1】:

这是因为您在while 循环内添加了figure('...') 行。所以它每次迭代都会打开一个新窗口。移动该行和scrsz=... 行并将其放在while t&lt;T 行的正上方(即在循环之外)。

要绘制到多个图形窗口,请使用轴句柄,如下所示:

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

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

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

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

【讨论】:

  • 如果我在同一个循环中有两个单独的绘图函数?我能以某种方式命名这个人物吗?
  • 我试图放置 'scrsz' 和 'h = figure('position',[80 60 scrsz(3)-110 scrsz(4)-150]);'在while循环上方,在循环'plot(h,time(i),configurations(1,1,i),'-b','LineWidth',2)中,坚持;'。没有成功。
  • 你确定你没有在一个地方命名为hAxes...,在其他地方命名为hAxis...?注意i 和e
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-05-28
  • 2012-04-26
  • 2015-07-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多