Ending a function after a time duration
조회 수: 6 (최근 30일)
이전 댓글 표시
I have a function that, for unknown reasons, is prone to stalling at a certain stage. I would like to force the function to return control to the caller if the function has not finished within, say, one second. I have tried executing variations on the following within the function, but none of them seem to actually return control to the caller. With the code printed below, I obtain the error given below. Any suggestions would be greatly appreciated.
t = timer('TimerFcn',evalin('caller','return'),'StartDelay',1);
start(t)
Error using searchmodels (line 172)
Error: Illegal use of reserved keyword "return".
댓글 수: 0
답변 (2개)
Chad Greene
2016년 6월 21일
What about this?
waitseconds = 2; % <-Enter number of seconds to wait.
starttime = now;
while now<starttime+waitseconds/86400
disp running
end
disp done
The now function gives the current time in units of days, so dividing by 86400 converts waitseconds to days.
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!