Matlab - 在图形上显示对象

Matlab - displaying an object on a graph

我有一个定时器的函数如下

HH = 0; MM = 0; SS = 0;
timer = sprintf('%02d:%02d:%02d',HH,MM,SS);
for p = 1:86400
    SS = SS + 1;
    if SS == 60
        MM = MM + 1;
        SS = 0;
        pause(0.01)
    end
    if MM == 60
        HH = HH + 1;
        MM = 0;
        pause(0.1)
    end
    HH;
end
disp(timer)

如何在不断更新的同时将其显示在图表上。无法使用 plot() 或 set() 函数让它工作。

您可能希望根据您希望计时器的外观来调整此示例的细节,请注意我只是为时钟的每一秒放置了一个 pause(0.1),这样您可以更快地观看它比您的示例中所示的实时时间。您还可以使用 text() 函数的不同参数调整时钟的 size/color。

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

for p = 1:86400
    SS = SS + 1;
    pause(0.1);  %% pause a fixed amount for each clock tick
    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;  %% clear previous clock display
    text(0.5,0.5,timer);   %% re-plot time to figure        
end