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 default size is -1, which means that the variable inherits its size based on the value of the Scope property:

ScopeDescription
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].

Before R2021b: MATLAB Function blocks output column vectors as one-dimensional data.

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. You can then use a single integer in Size to define the size of an output column vector. See Interpret output column vectors as one-dimensional data. (since R2021b)

If you do not know the upper limit of the dimension size you need, specify an unbounded dimension with Inf. (since R2023b) For example, if you do not know the upper bound of the columns, but you have a maximum size of two rows, set Size to [2 Inf]. To specify an unbounded dimension:

For more information, see Unbounded Variable-Size Signals and Unbounded Variable-Size Signal Basic Operations.

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

  • Numeric constants

  • The arithmetic operators +, -, *, 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 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 these expressions:

k+1
size(x)
min(size(y))

Custom Size Property Example

This example uses a MATLAB Function block with an input variable that adjusts its size based on the value of a base workspace variable.

The MATLAB function takes an input signal that changes size based on the value of the base workspace variable, k. k is an array that the model defines when you open it. A Gain block multiplies the input signal by k and outputs this signal to the MATLAB Function block. To match the size of the input variable to the size of the input signal, the MATLAB Function block assigns a custom size to the input variable, u. Open the MATLAB Function block to view the variables and code.

function y = fcn(u)
y = 2*u;

View the variable properties. In the Function tab, click Edit Data. In the Symbols pane, select u to view the properties in the Property Inspector. To match the size of the input, the block defines u with the Size property set to size(k).

If you change the size of k, u matches the new size of k.

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