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 = varsizeError1(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 = varsizeExample1(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
You can also use explicit indexing to resolve errors when the variables are not
scalars. For example, this function defines values over a fixed range, but code
generation fails because the code generator cannot determine that the range
offset:(offset+9) is a fixed
size.
function output = varsizeError2(n) output = zeros(1,10); if numel(output) > 1 offset = n; output = offset:(offset+9); end end
To resolve this error,explicitly assign values to the output
variable to force the code generator to recognize that
offset:(offset+9) has a fixed
size.
function output = varsizeExample2(n) output = zeros(1,10); if numel(output) > 1 offset = n; output(:) = offset:(offset+9); end end
Allow the Variable on the Left Side to Vary by Using coder.varsize
Use coder.varsize 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 = varsizeError3(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 = varsizeExample3(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, or for this operation by using coder.sameSizeBinaryOp. 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 | coder.noImplicitExpansionInFunction | coder.sameSizeBinaryOp