如何用Matlab实现倒计时

比如倒计时5分钟
2026-05-19 17:20:46
推荐回答(1个)
回答1:

拿这个去改改

%主程序

%Endtime是需要设置的结束时间——当结束时间早于当前时间时,倒计时程序将不起作用。

global Endtime
Endtime=[2013 10 22 21 15 00];
t = timer('StartDelay', 0,'Period',0.05,'TasksToExecute',
10000,...

'ExecutionMode','fixedRate');
t.TimerFcn = {@mycallback};
start(t)

%子函数

function
mycallback(t,events,arg_str)
%mycallback.m
global Endtime
Lefttime=etime(Endtime,clock);
if Lefttime<0
stop(t);
disp('剩余时间: 0分 0秒000')
return
end
LeftMillionSeconds=round(rem(Lefttime,1)*1000);
LeftSeconds=floor(rem(Lefttime,60));
LeftMinutes=floor(rem(Lefttime/60,60));
r=sprintf('剩余时间:%d分%d秒%d',LeftMinutes,LeftSeconds,LeftMillionSeconds);
disp(r)