Resolve Error: Arrays Have Incompatible Sizes - MATLAB & Simulink (original) (raw)
Issue
The code generator can produce array size incompatibility errors in either of these situations:
- Your code changes the size of a variable that the code generator previously defined as fixed size. This type of error can occur when you assign a value to a variable, when you pass a variable to a function, or when you return a variable from a function.
- Your code performs an operation on two arrays with incompatible sizes. Alternatively, your code performs an operation on two arrays with compatible sizes, but implicit expansion is disabled.
In these cases, the code generator produces one of these errors:
Arrays have incompatible sizes
Arrays in element '_`element`_' have incompatible sizes
Arrays in field '_`field`_' have incompatible sizes
Unable to perform this operation because the sizes of the arrays are incompatible
Possible Solutions
Depending on whether the code generator produces the error during variable assignment or during a binary (two-array) operation, try one of these solutions.
Instruct the Code Generator to Allow a Variable to Change Size by Using coder.varsize
If the error occurs when you assign a value to a variable, when you pass a variable to another function, or when you return a variable from a function, your code may change the size of a variable that the code generator previously defined as fixed size. In this situation, explicitly instruct the code generator to allow the variable to change size by using coder.varsize. See Address Size Incompatibility Error When Generating Code.
For more information about using variable-size arrays in MATLABĀ® code for code generation, see Generate Code for Variable-Size Arrays.
Perform Binary Operations on Arrays of Compatible Sizes
If the error occurs during a binary operation, check whether your code has a size incompatibility issue.
Most binary operators and functions in MATLAB support numeric arrays that have compatible sizes. Two inputs have compatible sizes if, for each dimension, the inputs are the same size or one of the dimensions is 1
. MATLAB implicitly expands arrays with compatible sizes to be the same size during the execution of element-wise operations or functions. See Compatible Array Sizes for Basic Operations.
By default, the code generator supports implicit expansion and binary operations on arrays of compatible size. However, the code generator produces size incompatibility errors if either of these conditions is true:
- Your code performs an operation on two arrays of incompatible sizes. This code also errors in MATLAB execution.
- Your code performs an operation on two arrays of compatible sizes, but implicit expansion is disabled.
To resolve this error, first run your code in MATLAB to verify that the arrays in binary operations have compatible sizes. Run your code successfully in MATLAB before you generate code.
If your code runs successfully in MATLAB, but the code generator produces a size incompatibility error, implicit expansion may be disabled. Depending on how your code disables implicit expansion, try one of these solutions:
- If you disable implicit expansion in a function by using coder.sameSizeBinaryOp, make sure that the inputs you pass to this function are the same size.
- If you disable implicit expansion in a function by using thecoder.noImplicitExpansionInFunction directive, make sure that all binary operations in the function are performed on inputs of the same size. Alternatively, perform these operations in another function where you do not disable implicit expansion.
- If you disable implicit expansion globally by using the Code Configuration dialog box or by using a code configuration object, but you only need to prevent implicit expansion for certain functions and operations, reenable implicit expansion globally. Then, disable implicit expansion for specific functions or operations by using
coder.sameSizeBinaryOp
orcoder.noImplicitExpansionInFunction
.
See Optimize Implicit Expansion in Generated Code.
See Also
coder.varsize | coder.noImplicitExpansionInFunction | coder.sameSizeBinaryOp