Ending a function after a time duration
6 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Nathanael Kazmierczak
el 21 de Jun. de 2016
Comentada: Nathanael Kazmierczak
el 21 de Jun. de 2016
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 comentarios
Respuestas (2)
Chad Greene
el 21 de Jun. de 2016
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.
Chad Greene
el 21 de Jun. de 2016
Another option is simply
pause(2)
to wait two seconds.
0 comentarios
Ver también
Categorías
Más información sobre Loops and Conditional Statements en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!