## Define and Modify Function Argument Types

Create function arguments for a MATLAB Function block by entering them in
the function declaration statement in the **MATLAB Function Block Editor**. When you
define arguments, Simulink^{®} creates corresponding ports on the MATLAB Function block that you
can attach to signals. You can select a data type mode for each argument that you define. Each
data type mode presents its own set of options.

By default, MATLAB Function blocks inherit their data types from the incoming or outgoing signal. To override the default type, choose a data type mode and then modify the associated properties.

### Specify Argument Types

To specify the type of a MATLAB Function block function argument:

Double-click the MATLAB Function block to open the

**MATLAB Function Block Editor**.Click

**Ports & Data Manager**to open the Ports and Data Manager.In the left pane, select the argument of interest.

In the

**Data**pane, click the Show data type assistant button to display the Data Type Assistant. Then, choose an option from the**Mode**drop-down menu.The

**Data**properties dialog box displays additional fields for each selection for**Mode**.Based on the mode you select, specify a desired data type:

Mode What to Specify `Inherit`

(default)The data type is inherited from previously-defined data, based on the

**Scope**you selected for the MATLAB Function block function argument:If

**Scope**is`Input`

, data type is inherited from the input signal on the designated port.If

**Scope**is`Output`

, data type is inherited from the output signal on the designated port.If

**Scope**is`Parameter`

, data type is inherited from the associated parameter, which can be defined in the Simulink masked subsystem or the MATLAB^{®}workspace.

`Built in`

Select from the drop-down list of supported data types, as described in Built-In Data Types for Arguments. `Fixed point`

Specify the fixed-point data properties as described in Specify Fixed-Point Designer Data Properties.

`Expression`

Enter an expression that evaluates to a data type, as described in Specify Argument Types with Expressions. `Bus Object`

In the

**Bus object**field, enter the name of a`Simulink.Bus`

object to define the properties of a MATLAB structure. You must define the bus object in the base workspace. See How Structure Inputs and Outputs Interface with Bus Signals.**Note**You can click the

**Edit**button to create or modify`Simulink.Bus`

objects using the Simulink Bus Editor (see Attach Bus Signals to MATLAB Function Blocks.`Enumerated`

In the Enumerated field, enter the name of a `Simulink.IntEnumType`

object that you define in the base workspace. See Code Generation for Enumerations.

### Inheriting Argument Data Types

MATLAB Function block function arguments can inherit their data types, including fixed point types, from their connected signals.

Select the argument of interest in the Ports and Data Manager

In the

**Data**properties dialog, select`Inherit: Same as Simulink`

from the**Type**drop-down menu.

An argument can also inherit complexity (whether its value is a real or complex number)
from the signal that is connected to it. To inherit complexity, set the **Complexity** field on the **Data**
properties dialog to **Inherited**.

**Note**

Library MATLAB Function blocks can have inherited data types, sizes, and complexities like ordinary MATLAB Function blocks. However, all instances of the library block in a given model must have inputs with the same properties.

After you build the model, the **CompiledType** column of
the Ports and Data Manager or Model Explorer gives the actual type inherited from
Simulink in the compiled simulation. The diagram actions that store values in the
specified output determine the inherited type of output data. If the expected type matches
the inferred type, inheritance is successful. In all other cases, a mismatch occurs during
build time.

### Built-In Data Types for Arguments

When you select **Built-in** for **Mode**, the
**Type** property dialog box provides a drop-down list of
supported data types. You can also choose a data type from the **DataType**
column in the Ports and Data Manager. The supported data types are:

Data Type | Description |
---|---|

| 64-bit double-precision floating point |

| 32-bit single-precision floating point |

| A half-precision data type occupies 16 bits of memory, but its floating-point representation enables it to handle wider dynamic ranges than integer or fixed-point data types of the same size. See The Half-Precision Data Type in Simulink (Fixed-Point Designer). |

| 64-bit signed integer |

| 32-bit signed integer |

| 16-bit signed integer |

| 8-bit signed integer |

| 64-bit unsigned integer |

| 32-bit unsigned integer |

| 16-bit unsigned integer |

| 8-bit unsigned integer |

| Boolean ( |

### Specify Argument Types with Expressions

You can specify the types of MATLAB Function block function arguments as expressions in the Ports and Data Manager.

Select

`<data type expression>`

from the**Type**drop-down menu of the Data properties dialog, or set the**Mode**to`Expression`

.In the

**Type**field, replace “” with an expression that evaluates to a data type. The following expressions are allowed:`<data type expression>`

Alias type from the MATLAB workspace, as described in

`Simulink.AliasType`

.`fixdt`

function to create a`Simulink.NumericType`

object describing a fixed-point or floating-point data type.`type`

(Stateflow) operator, to base the type on previously defined data.

### Specify Fixed-Point Designer Data Properties

MATLAB Function blocks can represent signals and parameter values as fixed-point numbers. To simulate models that use fixed-point data in MATLAB Function blocks, you must install Fixed-Point Designer™.

You can set the following fixed-point properties:

**Signedness.** Select whether you want the fixed-point data to be `Signed`

or `Unsigned`

. Signed data can represent positive and negative
quantities. Unsigned data represents positive values only. The default is
`Signed`

.

**Word length.** Specify the size (in bits) of the word that will hold the quantized integer. Large
word sizes represent large quantities with greater precision than small word sizes. Word
length can be any integer between 0 and 128 bits. The default is 16.

**Scaling.** Specify the method for scaling your fixed point data to avoid overflow conditions and
minimize quantization errors. You can select the following scaling modes:

Scaling Mode | Description |
---|---|

`Binary point` (default) | If you select this mode, the Data Type Assistant displays the
Binary points can be positive or negative integers. A positive integer moves the binary point left of the rightmost bit by that amount. For example, an entry of 2 sets the binary point in front of the second bit from the right. A negative integer moves the binary point further right of the rightmost bit by that amount, as in this example:
The default is 0. |

`Slope and bias` | If you select this mode, the Data Type Assistant displays fields for
entering the Slope can be any *positive*real number. The default is 1.0.Bias can be any real number. The default value is 0.0.
You can enter slope and bias as expressions that contain parameters defined in the MATLAB workspace. |

**Note**

You should use binary-point scaling whenever possible to simplify the implementation of fixed-point data in generated code. Operations with fixed-point data using binary-point scaling are performed with simple bit shifts and eliminate the expensive code implementations required for separate slope and bias values.

**Data type override.** Specify whether the data type override setting is `Inherit`

(default)
or `Off`

.

**Calculate Best-Precision Scaling.** The Simulink software can automatically calculate “best-precision” values
for both `Binary point`

and ```
Slope and
bias
```

scaling, based on the Limit range properties you specify.

To automatically calculate best precision scaling values:

Specify

**Minimum**,**Maximum**, or both Limit range properties.Click

**Calculate Best-Precision Scaling**.

The Simulink software calculates the scaling values, then displays them in either the
**Fraction Length**, or **Slope** and
**Bias** fields.

**Note**

The Limit range properties do not apply to data with the **Scope**
property set to `Constant`

or
`Parameter`

. Simulink cannot calculate best-precision scaling for these kinds of data.

**Fixed-point Details.** You can view the following Fixed-point details:

Fixed-point Detail | Description |
---|---|

Representable maximum | The maximum number that can be represented by the chosen data type, sign, word length and fraction length (or data type, sign, slope and bias). |

Maximum | The maximum value specified. |

Minimum | The minimum value specified. |

Representable minimum | The minimum number that can be represented by the chosen data type, sign, word length and fraction length (or data type, sign, slope and bias). |

Precision | The precision for the given word length and fraction length (or slope and bias). |

#### Using Data Type Override with the MATLAB Function Block

If you set the Data Type Override mode to `Double`

or
`Single`

in Simulink, the MATLAB Function block sets the type of all inherited
input signals and parameters to `fi double`

or ```
fi
single
```

objects respectively (see MATLAB Function Block with Data Type Override (Fixed-Point Designer) for more information). You must check the data types of your inherited input signals
and parameters and use the Ports and Data Manager to set explicit types for any inputs
that should not be fixed-point. Some operations, such as `sin`

, are not
applicable to fixed-point objects.

**Note**

If you do not set the correct input types explicitly, you may encounter compilation problems after setting Data Type Override.

To set Data Type Override, follow these steps:

In the Simulink

**Apps**tab, select**Fixed-Point Tool**.Set the value of the

**Data type override**parameter to`Double`

or`Single`

.