MATLAB Answers

0

Is there a way for breaking a loop iteration, if it takes to long?

Asked by Philipp on 7 Jun 2016
Latest activity Commented on by Jingying Wang on 14 Nov 2017
Dear People, I am running analyzes of multiple input data files. Each file is analyzed in one iteration of a for loop, e.g.
for 1:length(data)
analyze(data{i});
end
However, for some data, the analysis takes very long, because the RAM is full. Is there a way of breaking the loop on a timer or RAM condition, say after 1 hour of iteration time, or as RAM gets full? Or do you have any other recommendation, except for throughing the data away in advance, or tuning the analysis?
Kind regards, Philipp

  0 Comments

Sign in to comment.

2 Answers

Answer by Jan Orwat
on 7 Jun 2016

One of the simplest ways for time control is to use tic and toc
time0 = tic;
timeLimit = 60*60*1; % 1 hour == 3600 seconds
for ...
...
if toc(time0)>timeLimit
break
end
end
or using while loop
time0 = tic;
timeLimit = 60*60*1; % 1 hour == 3600 seconds
while conds && toc(time0)<timeLimit
...
end
in case of placing multiple breakpoints it is vital to identify them e.g. by printing breakpoint info. It is crucial to design breakpoints in a way allowing to resume computations from the point it was disrupted. Sometimes instead of command break it is better to put a message and a debbuger breakpoint which will give you command line access while program is paused. Then you can continue from this exact point in code.

  3 Comments

Dear Jan, thank you very much for this answer. However, I would be looking for a workaround that react on execution times of single functions without splitting the function up. In my example, the execution of analyze(data{i}) takes very long. Kind regards, Philipp
I thought it doesn't split the function. This wouldn't work for you?
t0 = toc;
tLimit = 3600;
for i = 1:length(data),
analyze(data{i});
if toc(t0)>tLimit, break; end
end
You can return later changing one line:
for i = whereIStopped:length(data)
Or do you want to stop the function "analyze" from further operation if it takes too long, and switch it off during operation?
I have the same Problem but I want to stop the function "analyze", or better to break the "for" circle. You know how?

Sign in to comment.


Answer by Jos (10584)
on 7 Jun 2016
Edited by Jos (10584)
on 7 Jun 2016

You can create a timer ;
th = timer('TimerFcn','keyboard') ;
th.StartDelay = 5 ;
start(th) ;
pause ; % will be stopped after 5 seconds.
stop(th) ;

  0 Comments

Sign in to comment.