【问题标题】:Matlab - updating objects on a plotMatlab - 更新绘图上的对象
【发布时间】:2016-05-18 12:40:15
【问题描述】:

我正在尝试添加一个计时器来模拟我正在进行的模拟。目前我可以让计时器显示在我想要的位置,但是我不能让数字相互清除,即它们都只是慢慢地堆叠在一起,形成一个纯黑色的混乱。我试过实现一个 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

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

【问题讨论】:

    标签: matlab plot timer simulation figure


    【解决方案1】:

    您希望存储handle to the text object 并更新此现有对象的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
    

    【讨论】:

    • 该死的@Suever,你速度很快
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多