Main Content

Nonconstant Index into varargin or varargout in a for-Loop

Issue

Your MATLAB® code contains a for-loop that indexes into varargin or varargout. When you generate code, you see this error message:

Non-constant expression or empty matrix. This expression
must be constant because its value determines the size
or class of some expression.

Cause

At code generation time, the code generator must be able to determine the value of an index into varargin or varagout. When varargin or varagout are indexed in a for-loop, the code generator determines the index value for each loop iteration by unrolling the loop. Loop unrolling makes a copy of the loop body for each loop iteration. In each iteration, the code generator determines the value of the index from the loop counter.

The code generator is unable to determine the value of an index into varargin or varagout when:

  • The number of copies of the loop body exceeds the limit for loop unrolling.

  • Heuristics fail to identify that loop unrolling is warranted for a particular for-loop. For example, consider the following function:

    function [x,y,z] = fcn(a,b,c)
    %#codegen
    
    [x,y,z] = subfcn(a,b,c);
    
    function varargout = subfcn(varargin)
    j = 0;
    for i = 1:nargin
        j = j+1;
        varargout{j} = varargin{j};
    end

    The heuristics do not detect the relationship between the index j and the loop counter i. Therefore, the code generator does not unroll the for-loop.

Solution

Use one of these solutions:

Force Loop Unrolling

Force loop unrolling by using coder.unroll. For example:

function [x,y,z] = fcn(a,b,c)
%#codegen
[x,y,z] = subfcn(a,b,c);

function varargout = subfcn(varargin)
j = 0;

coder.unroll();
for i = 1:nargin
    j = j + 1;
    varargout{j} = varargin{j};
end

Rewrite the Code

Rewrite the code so that the code generator can detect the relationship between the index and the loop counter. For example:

function [x,y,z] = fcn(a,b,c)
%#codegen
[x,y,z] = subfcn(a,b,c);

function varargout = subfcn(varargin)
for i = 1:nargin
    varargout{i} = varargin{i};
end

See Also

Related Topics