For loop: max iterations exceeded
8 views (last 30 days)
Show older comments
I'm trying to run a simple for loop that iterates through each record of a CSV (which has 185 records) and does things with it. But I get the error "maximum number of iterations reached".
rrs_chl = readtable('filename.csv');
for i = 1:height(rrs_chl)
...
<run functions>
...
end
Exiting: Maximum number of iterations has been exceeded
- increase MaxIter option.
Current function value: 0.015808
I'm not sure how to resolve this. I am assuming it stems from the for loop.
I tried
options = struct('MaxFunEvals', 2000)
and also
options = optimset('MaxIter',2000)
but I'm still getting the same error. I am using Matlab R2015a 64-bit.
0 Comments
Answers (2)
Kelly Kearney
on 8 Jun 2017
That message typically comes from optimization routines, and it indicates that it wasn't able to meet the optimization criteria in the specified number of internal iterations. It has nothing to do with your for loop, so we're going to need to know what's going on the the body of the for loop.
3 Comments
dpb
on 8 Jun 2017
It's fminsearch (or one of the other similar iterative solvers) that is throwing the error, indeed.
gopt looks like the local structure holding the options. MaxIter is a field therein; you can try setting it up to something larger but as noted earlier it's likely more fundamental that if you do some other issue will arise.
Without the more extensive code and data don't know there's much more anybody here can tell you other than you have found the culprit. As the old "Mission Impossible" line went, "Your mission should you choose to accept it.." is to now look at the details of the problem and see what would need to do to get convergence.
dpb
on 8 Jun 2017
Has nothing whatsoever to do with the for loop; it is in the inner iteration of whatever function it is that you're calling.
We can't tell you the solution altho the error message is a hint; perhaps raising that iteration limit might let it converge; often that's hiding a problem in the problem statement that just can't find a solution as posed.
Anyway, the problem is in what you did NOT show; you'll find you'll have the same error if you just
<run functions>
from the command line with the same inputs set as you have in the script.
0 Comments
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!