2011-08-22 171 views
-2

爲什麼我不能在同一窗口中的每次迭代中繪製數據?我嘗試了drawnow,但它不起作用。代碼:在Matlab中繪圖'while'循環

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 
+0

請定義「不工作」。 –

+0

它在每次迭代中將數據繪製在新窗口中。最後我有10個繪圖窗口。 – Makaroni

回答

2

那是因爲你已經添加了figure('...')while循環。所以它每次迭代都會打開一個新窗口。將該行和scrsz=...行移動並將其放在while t<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 
+0

如果我在同一個循環內有兩個單獨的繪圖函數?我能不能簡單地說出這個數字? – Makaroni

+0

我試圖把'scrsz'和'h = figure('position',[80 60 scrsz(3)-110 scrsz(4)-150]);'' (h,時間(i),配置(1,1,i),' - b','LineWidth',2),保持;')。沒有成功。 – Makaroni

+0

@Makaroni現在查看我的編輯。 – abcd