Matlab - 更新绘图上的对象

Matlab - updating objects on a plot

我正在尝试将计时器添加到我正在处理的模拟中。目前我可以让计时器显示在我想要的位置,但是我无法让数字彼此清晰,即它们都只是相互堆叠,慢慢地形成一团黑色的混乱。我试过实现一个 clf 函数,但它只是清除了整个图形。计时器的代码是:

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

有没有办法只重置定时器功能使其正常显示?

您想存储此现有对象的 handle to the text object and update the String 属性 而不是每次都创建一个新的 text 对象。

%// 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 对象执行类似的操作,而不是每次迭代都清除图形并重新绘制所有绘图。

将此与您的代码集成,我们得到如下内容:

%// 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