How can I abort a extensive loop after a given amount of time in MATLAB 7.6 (R2008a)?
10 views (last 30 days)
Show older comments
I have a large FOR loop in my code that should be aborted after a certain amount of time has elapsed.
Accepted Answer
MathWorks Support Team
on 27 Jun 2009
This can be done by including a conditional IF statement in the loop that needs to be aborted after a certain time of computation has been exeeded. The example below uses the CLOCK and ETIME functions to compute the time and abort the loop with BREAK and END statements.
% Example - terminate loop after 60 seconds
t1=clock;
for i = 1:n
if (etime(clock,t1)>60), break, end
%do somthing
end
You could also work with RETURN or ERROR functions instead of the BREAK statement.
0 Comments
More Answers (0)
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!