开发者

Plotting in Matlab 'while' loop

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


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜