timer never stop!

2 ビュー (過去 30 日間)
Kyungsun Ha
Kyungsun Ha 2017 年 3 月 14 日
編集済み: Jan 2017 年 3 月 14 日
function timecount(~)
sec=input('얼마나 세야 하는거냐?');
t = timer('TimerFcn', 'stat=false; disp(''땡!'')','StartDelay',sec);
start(t)
stat=true;
b=sec;
a=0;
min=0;
hour=0;
while(stat==true)
b=b-1;
a=a+1;
if a>=60
a=a-60;
min=min+1;
end
if min>=60
min=min-60;
hour=hour+1;
end
if min==0
fprintf('%s초 \n', num2str(a));
elseif hour==0
fprintf('%s분 \n', num2str(min));
fprintf('%s초 \n', num2str(a));
else
fprintf('%s시간 \n', num2str(hour));
fprintf('%s분 \n', num2str(min));
fprintf('%s초 \n', num2str(a));
end
pause(1)
if sec == 0;
stop(t)
delete(t)
end
end
end
this is whole code of timer.. Input is second that you want to count(ex : 10sec).. when you input 10sec, this function will end after 10sec. but it's not stop and run infinity...

採用された回答

Jan
Jan 2017 年 3 月 14 日
編集済み: Jan 2017 年 3 月 14 日
When you define the timer's callback as a string, it is evaluatred in the base workspace. Therefore you cannot observe changes in the variable |stat|inside the show function. Better use a flag which is "attached" to the timer, e.g. its UserData:
t = timer('TimerFcn', @myTimerCB, 'StartDelay', sec, 'UserData', 0);
start(t)
...
while get(t, 'UserData') == 0
...
end
...
function mytimerCB(TimerH, EventData)
set(TimerH, 'UserData', 1);
end

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeWorkspace Variables and MAT-Files についてさらに検索

タグ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by