Resolve Error: Fixed Size on the Left Side but Variable Size on the Right
Issue
For code generation, a variable is either fixed size or variable size. If the code generator can determine that the size of a variable does not change, it fixes the size of the variable in the generated code. Otherwise, it allows the variable to change size. If you assign a variable-size value to a variable that the code generator previously defined as fixed size, the code generator produces one of these errors:
Unable to make this
assignment because dimension
dimension
is fixed size
on the left side but variable size on the right
Unable to make this
assignment because dimension
dimension
in element
'element
' is fixed size on the left side but
variable size on the right
Unable to make this
assignment because dimension
dimension
in field
'field
' is fixed size on the left side but
variable size on the right
Possible Solutions
To resolve this error, try one of these solutions.
Force the Variables to Be the Same Size by Using Explicit Indexing
If you do not need the variable on the right side to be variable size, you can
force the variable to be fixed size by using explicit indexing. For example,
suppose you have a function that defines variables A
and
B
. A
is a variable-size scalar and
B
is a fixed-size scalar. Code generation fails because
the function tries to assign the variable-size variable A
to
the fixed-size variable B
.
function out = varsizeError2(n) %#codegen coder.varsize("A",[1 1],[true true]); A = ones(n,n); B = 3; numel(B); if (n == 3) B = A; end out = B; end
To generate code for this function, use explicit indexing to force
A
to be a fixed-size
scalar.
function out = varsizeExample2(n) %#codegen coder.varsize("A",[1 1],[true true]); A = ones(n,n); B = 3; numel(B); if (n == 3) B = A(1); end out = B; end
Allow the Variable on the Left Side to Vary by Using coder.varsize
Use coder.varsize
(MATLAB Coder) to instruct the code
generator to treat the variable on the left side of the assignment as variable
size. For example, consider this
function:
function out = varsizeError1(n) %#codegen A = ones(n,n); B = magic(3); numel(B); if (n == 3) B = A; end out = B; end
The code generator recognizes array A
as variable size
because the size of A
depends on the input value. The code
generator identifies B
as fixed size because the size of this
array is constant when the code uses B
by passing it to the
numel
function. Code generation fails for the assignment
B = A
because A
is variable size and
B
is fixed size. To resolve this error, use
coder.varsize
to instruct the code generator to allow the
size of B
to
vary.
function out = varsizeExample1(n) %#codegen A = ones(n,n); B = magic(3); coder.varsize("B"); numel(B); if (n == 3) B = A; end out = B; end
Prevent Implicit Expansion
When performing binary operations on arrays of different but compatible sizes, the code generator implicitly expands the arrays so that they are the same size. If you assign the implicitly expanded output of a binary operation or function to a fixed-size variable of different size, the code generator might produce an error. For example, consider this function:
function out = varsizeError3(n) %#codegen A = ones(n,1); B = ones(n,n); if n == 1 A = A+B; end out = A; end
The second dimension of array A
is fixed size. To perform
the operation A+B
, the code generator implicitly expands the
second dimension of A
to match the second dimension of
B
. The result of A+B
is an array that
is variable size in both dimensions. Code generation fails because the code
generator cannot assign an variable with a second dimension that is variable
size to an variable with a second dimension that is fixed size. In this example,
the operation A+B
only executes if both arrays are 1-by-1.
Therefore, you can disable implicit expansion for this function by using
coder.noImplicitExpansionInFunction
(MATLAB Coder), or for this operation by
using coder.sameSizeBinaryOp
(MATLAB Coder). For
example:
function out = varsizeExample3(n) %#codegen coder.noImplicitExpansionInFunction A = ones(n,1); B = ones(n,n); if n == 1 A = A+B; end out = A; end
See Also
coder.varsize
(MATLAB Coder) | coder.noImplicitExpansionInFunction
(MATLAB Coder) | coder.sameSizeBinaryOp
(MATLAB Coder)
Topics
- Define Variable-Size Data for Code Generation (MATLAB Coder)
- Optimize Implicit Expansion in Generated Code (MATLAB Coder)
- Resolve Error: Arrays Have Incompatible Sizes (MATLAB Coder)