Main Content

Resolve Error: Cannot Determine the Exact Number of Iterations for a Loop

Issue

When the number of for-loop iterations calculated by the generated code does not match the number of for-loop iterations calculated by MATLAB®, the code generator produces this error when you call the generated MEX function:

Cannot determine the exact number of iterations for a loop with range {initVal}:{step}:{endVal}.

Possible Solutions

Reduce Number of Loop Iterations

Code generation does not support loops with more than intmax("uint32") iterations. For example, consider this function:

function out = myOpenEndLoop(endval) %#codegen
out = 0;
for i = 1:endval 
    out = out+1;
end
end

Generate a MEX function for myOpenEndLoop and call the MEX function with an input value greater than intmax("uint32"). The MEX function produces a run-time error.

myOpenEndLoop_mex(double(intmax("uint32"))+1)
Cannot determine the exact number of iterations for a loop with range 1:1:4.29497e+09.

To resolve this error, split the loops in the MATLAB function such that the maximum loop index is less than intmax("uint32"). For example:

function out = myLoopChunk(endval) %#codegen
out = 0;
maxIter = double(intmax("uint32"));

numChunks = floor(endval/maxIter); 
remainder = mod(endval,maxIter); 

for chunk = 1:numChunks
    out = out+maxIter;
end
out = out+remainder;
end

Avoid Loop Variables With Fractional Values

The code generator calculates the number of loop iterations by using a different algorithm than MATLAB. For for-loops with step sizes, initial values, or end values that are not integers, round-off error can cause the number of iterations calculated by the generated code to differ from the number of loop iterations calculated by MATLAB. In this case, the generated MEX function produces an error at run time. For example, consider this MATLAB function:

function out = myVarLoop(startVal,stepVal,endVal) %#codegen
out = 0;
for i = startVal:stepVal:endVal
    out = [out i];
end
end

Generate a MEX function for myVarLoop and call the MEX function with fractional values. For most fractional input values, the MEX function produces a run-time error.

myVarLoop_mex(19.8,0.8,39.8)

Cannot determine the exact number of iterations for a loop with range 19.8:0.8:39.8.

To resolve this issue, use integer loop variables or iterate over a vector.

Use integer step sizes and loop bounds.  Convert non-integer step sizes, initial values, and end values to integers. First, determine the number of iterations, which can usually be calculated by using the formula iterations = 1+round((endVal-startVal)/stepVal). Then, extract the fractional values from the loop control statement. For example:

function out = myIntLoop(startVal,stepVal,endVal) %#codegen
out = 0;
iterations = 1+round((endVal-startVal)/stepVal);
for j = 1:iterations
    i = startVal+(j-1)*stepVal;
    out = [out i];
end
end

Iterate over a vector.  Store the loop values in a vector and then iterate over the vector. For example:

function out = myVectorLoop(startVal,stepVal,endVal) %#codegen
out = 0;
loopVec = startVal:stepVal:endVal;
for i = 1:numel(loopVec)
    out = [out loopVec(i)]
end
end

See Also

|

Topics