MATLAB: How to abort a extensive loop after a given amount of time in MATLAB 7.6 (R2008a)

abortbreakloopMATLABterminatetimetimer

I have a large FOR loop in my code that should be aborted after a certain amount of time has elapsed.

Best Answer

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.
Related Question