Define and Modify Variable Data Types - MATLAB & Simulink (original) (raw)

When you create variables in a MATLAB Function block, you can use theType property to set the data type. Variables can inherit their data types, or be set to built-in, fixed-point, or enumerated data types. Variables can also be nonvirtual buses. By default, MATLAB Function block variables inherit their data type.

For more information on creating variables, see Create and Define MATLAB Function Block Variables.

Specify Variable Data Types

You can specify the data types by using theSymbols pane and Property Inspector (since R2022a), or the Model Explorer.

To specify the data type using the Symbols pane and Property Inspector:

  1. Double-click the MATLAB Function block to open theMATLAB Function Block Editor.
    This image shows the MATLAB Function Block Editor that opens for a new MATLAB Function block. The Function tab is selected in the Simulink Editor.
  2. In the Function tab, click Edit Data.
  3. In the Symbols pane, select the variable.
  4. In the Property Inspector, in the Properties tab, select the data type from the Type property.

To specify the data type of a variable by using the Model Explorer:

  1. Open the Model Explorer. In the Modeling tab, in theDesign section, click Model Explorer.
  2. In the Model Hierarchy pane select theMATLAB Function block.
  3. Click the variable you want to modify.
  4. Select the data type from the Type property.

In the Model Explorer, you can also filter the data type options. In theGeneral tab, click the Show data type assistant button to display the Data Type Assistant parameters. Then, choose an option from the Mode parameter. Based on the mode you select, specify the data type:

Mode What to Specify
Inherit (default) The inherited data depends on the Scope property: If Scope is Input, the data type is inherited from the input signal on the designated port.If Scope is Output, the data type is inherited from the output signal on the designated port.If Scope is Parameter, the 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 a list of built-in data types.
Fixed point Specify the fixed-point data properties.
Enumerated Enter the name of a Simulink.IntEnumType object that you define in the base workspace. See Code Generation for Enumerations.
Bus Object In the Bus object field, enter the name of aSimulink.Bus object to define the properties of a MATLAB structure. You must define the bus object in the base workspace. See Create Structures in MATLAB Function Blocks. NoteYou can click the Edit button to create or modify Simulink.Bus objects by using the Simulink Type Editor.
Expression Enter an expression that evaluates to a data type.

Inheriting Data Types

MATLAB Function block variables can inherit their data types, including fixed point types, from their connected signals. To make a variable inherit a data type, set the Type property to Inherit: Same as Simulink.

An argument can also inherit complexity from the signal connected to it. To inherit complexity, set the Complexity property to .

After you build the model, the CompiledType column of the Model Explorer gives the actual type inherited from Simulink. If the expected type matches the inferred type, inheritance is successful.

Built-In Data Types

In the Model Explorer, when you expand the Data Type Assistant and setMode to Built in, you can setType to these built-in data types. The built-in data types are:

Data Type Description
double 64-bit double-precision floating point
single 32-bit single-precision floating point
half 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).
int64 64-bit signed integer
int32 32-bit signed integer
int16 16-bit signed integer
int8 8-bit signed integer
uint64 64-bit unsigned integer
uint32 32-bit unsigned integer
uint16 16-bit unsigned integer
uint8 8-bit unsigned integer
boolean Boolean
string String scalar

Fixed-Point Designer Data Type Properties

To represent variables as fixed-point numbers 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 variable to beSigned or Unsigned. Signed variables can represent positive and negative quantities. Unsigned variables 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 65,535 bits. The default is 16.

Scaling

Specify the method for scaling your fixed-point variable to avoid overflow conditions and minimize quantization issues. You can select these scaling modes:

Scaling Mode Description
Binary point (default) The Data Type Assistant displays the Fraction Length parameter, which specifies the binary point location.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 The Data Type Assistant displays the Slope andBias parameters:Slope can be any positive real number. The default is1.0.Bias can be any real number. The default value is0.0. You can enter slope and bias as expressions that contain parameters defined in the MATLAB workspace.

Note

Use binary-point scaling whenever possible to simplify the implementation of fixed-point numbers in generated code. Operations with fixed-point numbers that use 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 orOff. For more information, see Data Type Override with MATLAB Function Block (Fixed-Point Designer).

Calculate Best-Precision Scaling

Have Simulink automatically calculate best-precision values for both Binary point and Slope and bias scaling, based on theMinimum and Maximum properties you specify.

To automatically calculate best precision scaling values:

  1. Specify the Minimum or Maximum properties.
  2. Click Calculate Best-Precision Scaling.

Simulink calculates the scaling values, then displays them in either theFraction length, or the Slope andBias fields.

Note

The Minimum and Maximum properties do not apply to variables with the Scope property set toConstant or Parameter. The software cannot calculate best-precision scaling for these kinds of variables.

Fixed-point details

Displays information about the fixed-point variable that is defined in the Data Type Assistant:

This image shows the fixed-point details for variable named data.

If the value of a field cannot be determined without first compiling the model, theFixed-point details subpane shows the value asUnknown. The values displayed by the Fixed-point details subpane do not automatically update if you change the values that define the fixed-point variable. To update the values shown in the Fixed-point details subpane, click Refresh Details.

Clicking Refresh Details does not modify the variable. It changes only the display. To apply the displayed values, clickApply or OK.

The Fixed-point details subpane indicates issues resulting from the fixed-point variable specification. For example, this figure shows two issues.

This image shows the fixed-point details for data named data with two issues.

The row labeled Maximum indicates that the value specified by theMaximum property is not representable by the fixed-point variable. To correct the issue, make one of these modifications so the fixed-point data type can represent the maximum value:

The row labeled Minimum shows the message Cannot evaluate because evaluating the expression MySymbol, specified by the Minimum property, does not return a numeric value. When an expression does not evaluate successfully, the Fixed-point details subpane shows the unevaluated expression (truncating to 10 characters as needed) in place of the unavailable value. To correct this issue, defineMySymbol in the base workspace to provide a numeric value.

If you click Refresh Details, the issue indicator and description are removed and the value of MySymbol appears in place of the unevaluated text.

Specify Data Types with Expressions

You can specify the types of MATLAB Function block variables as expressions by using the Model Explorer or the Property Inspector.

To use the Model Explorer, set the Mode property toExpression. In the Type property, replace_<data type expression>_ with an expression that evaluates to a data type.

To use the Property Inspector, double-click the Type property, clear the contents, and enter an expression.

You can use the following expressions:

For example, suppose you want to designate the workspace variablemyDataType as an alias for a single data type to use as an expression in the Type property of an MATLAB Function block input variable. Create an instance of theSimulink.AliasType class and set its BaseType property by entering these commands:

myDataType = Simulink.AliasType; myDataType.BaseType = "single";

In the Property Inspector, enter the data type alias name,myDataType, as the value in the Type property.

This image shows the property inspector after entering the expression for the data type.

Programmatically Change Variable Data Types

You can change MATLAB Function block variable data types programmatically by retrieving the Stateflow.EMChart object of the block. For more information, see Configure MATLAB Function Blocks Programmatically.

See Also

MATLAB Function Block Editor

Topics