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:

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:

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:

See Optimize Implicit Expansion in Generated Code.

See Also

coder.varsize | coder.noImplicitExpansionInFunction | coder.sameSizeBinaryOp

Topics