Main Content

Specify Size of MATLAB Function Block Variables

You can specify the size of the variables in MATLAB Function blocks with the Symbols pane and Property Inspector, or with the Model Explorer. MATLAB Function blocks can use scalars, vectors, or matrices. For more information about creating, deleting, and setting properties for variables, see Create and Define MATLAB Function Block Variables and Define and Modify Variable Data Types. You can set a variable to inherit the size or manually specify the size.

Inherit Size from Simulink

The size defaults to -1, which means that the variable inherits its size based on the value of the Scope property:

InputInherits size from the Simulink® signal connected to the associated input port.
OutputInherits size from the Simulink signal connected to the associated output port.
ParameterInherits size from the associated Simulink or MATLAB® parameter. See Configure MATLAB Function Block Parameter Variables.

Variables with the Scope property set to Data Store Memory always inherent their size.

Customize Variable Sizes

You can also manually set the size of variables to a scalar, vector, or matrix. To specify the size as a scalar, set Size to 1 or clear the parameter. To specify the size as a vector or a matrix, enter a row vector with positive integers in [row column] format. For example, to define a column vector of size 6, set the Size property to [6 1]. To define a row vector of size 5, set the Size property to [1 5]. To define a matrix of data size 3-by-3, set the Size property to [3 3].


Prior to R2021b, MATLAB Function blocks output column vectors as one-dimensional data. In R2021b and later, MATLAB Function blocks retain their dimensionality by default. To maintain the old behavior, clear the Interpret output column vectors as one-dimensional data property in the Property Inspector or Model Explorer. Once you clear this property, you can then use a single integer in Size to define the size of an output column vector size. See Interpret output column vectors as one-dimensional data.

You can also set the Size property with an expression. The expressions can include:

  • Numeric constants

  • Arithmetic operators, restricted to +, -, *, and /

  • Names of variables with the Scope property set to Parameter

  • Calls to the MATLAB functions min, max, and size

These expressions must output a positive integer or two positive integers entered in [row column] format. Otherwise, the specified size produces an error at model compilation. For example, consider a MATLAB Function block with variables k, x, and y that all have the Scope property set to Parameter. Here, k is a positive integer and x and y are matrices. If you create new variables for the block, you can define the Size property with any of the following expressions:


Simulation and Size Matching

After you build the model, the CompiledSize column in the Model Explorer displays the actual size used in the simulation. If the value of the Size property is not -1 and does not match the actual size, a mismatch error occurs during model compilation.

See Also

Related Topics