How to stop execution of a program through code?

5 ビュー (過去 30 日間)
Elysi Cochin
Elysi Cochin 2021 年 3 月 31 日
編集済み: Jan 2021 年 3 月 31 日
When i execute a program, if the execution exceeds 2 minutes, i wanted to stop the execution through code. What should i do to check time and stop if it exceeds a certain time?

採用された回答

Jan
Jan 2021 年 3 月 31 日
編集済み: Jan 2021 年 3 月 31 日
tic;
while toc < 120
figure;
axes;
plot(1:1000, rand(1, 1000));
pause(0.5);
delete(gcf);
end
Or equivalently:
startTime = clock;
while etime(clock, startTime) < 120
...
end
Another approach is a timer object:
TimerH = timer('ExecutionMode', 'singleShot', ...
'TimerFcn', @myCallback, ...
'StartDelay', 120);
myCallback('reset');
start(TimerH);
while true
pause(1.0)
disp(clock)
if myCallback('get')
break;
end
end
function Reply = myCallback(Arg1, EventData)
persistent Triggered
if ischar(Arg1)
switch Arg1
case 'reset'
Triggered = false;
case 'get'
Reply = any(Triggered);
end
return;
end
Triggered = true;
end

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeEntering Commands についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by