2016-05-18 8 views
0

私が作業しているシミュレーションをタイマーに追加しようとしています。現時点では、私が望む場所に表示するタイマーを得ることができますが、数字を互いにクリアにすることはできません。つまり、お互いの上に積み重ねて、ゆっくりとしっかりした黒い混乱を作り出します。私はclf関数を実装しようとしましたが、それは全体のフィギュアをクリアします。タイマーのコードは次のとおりです。Matlab - プロット上のオブジェクトを更新する

HH = 0; MM = 0; SS = 0; 
timer = sprintf('%02d:%02d:%02d',HH,MM,SS); 
text(-450,450,timer); %% adjust location of clock in graph using the first two arguments: (x,y) coordinates 

for t = 1:86400 

    SS = SS + 1; 
    if SS == 60 
     MM = MM + 1; 
     SS = 0; 
    end 
    if MM == 60 
     HH = HH + 1; 
     MM = 0; 
    end 
    timer = sprintf('%02d:%02d:%02d',HH,MM,SS); %% construct time string after all adjustments to HH, MM, SS 
    clf(f,'reset'); %% clear previous clock display 
    text(-450,450,timer); %% re-plot time to figure 

    if t == EventTimes(1) 
     uav1 = uav1.uavSetDestin([event1(2:3) 0]); 
     plot(event1(2),event1(3),'+') 
     hold on 
    end 
    if t == EventTimes(2) 
     uav2 = uav2.uavSetDestin([event2(2:3) 0]); 
     plot(event2(2),event2(3),'r+') 
     hold on 
    end 

タイマー機能のみをリセットして正しく表示する方法はありますか?

答えて

1

新しいtextオブジェクトを毎回作成するのではなく、handle to the text objectを保存し、この既存のオブジェクトのStringプロパティを更新します。

%// The first time through your loop 
htext = text(-450, 450, timer); 

%// Every other time through the loop 
set(htext, 'String', sprintf('%02d:%02d:%02d',HH,MM,SS) 

また、むしろ数字をクリアし、プロットのすべてにすべての反復を再描画するよりも、plotオブジェクトと似た何かをしたいと思うでしょう。あなたは高速だくそ@Suever

%// Create the initial text object 
HH = 0; MM = 0; SS = 0; 
timerString = sprintf('%02d:%02d:%02d',HH,MM,SS); 
htext = text(-450, 450, timeString); 

%// Create the plot objects 
hplot1 = plot(NaN, NaN, '+'); 
hplot2 = plot(NaN, NaN, 'r+'); 

for t = 1:86400 
    SS = SS + 1; 

    %// I could help myself and made this significantly shorter 
    MM = MM + (mod(SS, 60) == 0); 
    HH = HH + (mod(MM, 60) == 0); 

    %// Update the timer string 
    timerString = sprintf('%02d:%02d:%02d',HH,MM,SS); 
    set(htext, 'String', timerString); 

    %// Update your plots depending upon which EventTimes() 
    if t == EventTimes(1) 
     uav1 = uav1.uavSetDestin([event1(2:3) 0]); 
     set(hplot1, 'XData', event1(2), 'YData', event1(3)); 
    elseif t == EventTimes(2) 
     uav2 = uav2.uavSetDestin([event2(2:3) 0]); 
     set(hplot2, 'XData', event2(2), 'YData', event2(3)); 
    end 

    %// Force a redraw event 
    drawnow; 
end 
+0

:私たちはのような何かを得るあなたのコードでこれを統合

BillBokeey

関連する問題