Main Content

Specify Type of Stateflow Data

The term data type refers to how computers represent information in memory. The data type determines the amount of storage allocated to data, the method of encoding a data value as a pattern of binary digits, and the operations available for manipulating the data.

Specify Data Type by Using the Data Type Assistant

You can specify the type of a data object in either the Property Inspector or the Model Explorer. In the Type field, select a type from the drop-down list or enter an expression that evaluates to a data type. For more information, see Set Data Properties.

Alternatively, use the Data Type Assistant to specify a data Mode and select the data type based on that mode:

  1. In the Model Explorer, on the Data pane, click the Show data type assistant button .

  2. Select a Mode from the drop-down list. The list of available modes depends on the scope of the data object.

    ScopeModes
    LocalInherit (available only in charts that use MATLAB® as the action language), Built in, Fixed point, Enumerated, Bus object, Expression
    ConstantBuilt in, Fixed point, Expression
    ParameterInherit, Built in, Fixed point, Enumerated, Bus object, Expression
    InputInherit, Built in, Fixed point, Enumerated, Bus object, Expression
    OutputInherit, Built in, Fixed point, Enumerated, Bus object, Expression
    Data Store MemoryInherit

  3. Specify additional information based on the mode. The Data Type Assistant populates the Type field based on your specification.

    ModeData Types
    Inherit

    You cannot specify a data type. You inherit the data type based on the scope that you select for the data object:

    For more information, see Inherit Data Types from Simulink Objects.

    Built in

    Specify a data type from the drop-down list of supported data types:

    • 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 (1 = true; 0 = false).

    • ml: Typed internally with the MATLAB array mxArray. Supported only in charts that use C as the action language. The ml data type provides Stateflow data with the benefits of the MATLAB environment, including the ability to assign the Stateflow data object to a MATLAB variable or pass it as an argument to a MATLAB function. ml data cannot have a scope outside the Stateflow hierarchy. That is, it cannot have a scope of Input or Output. For more information, see ml Data Type.

    • string: String. For more information, see Manage Textual Information by Using Strings.

    Fixed point

    Specify this information about the fixed-point data:

    • Signedness: Whether the data is signed or unsigned

    • Word length: Bit size of the word that holds the quantized integer. Large word sizes represent large values with greater precision than small word sizes. The default value is 16.

    • Scaling: Method for scaling your fixed-point data to avoid overflow conditions and minimize quantization errors. The default method is Binary point.

    For information, see Fixed-Point Data Properties.

    Enumerated

    Specify the class name for the enumerated data type. For more information, see Define Enumerated Data Types.

    Bus object

    Specify the name of a Simulink.Bus object to associate with the Stateflow® bus object structure. Click Edit to create or edit a bus object in the Type Editor. You can also inherit bus object properties from Simulink signals.

    Expression

    Specify an expression that evaluates to a data type. Use one of these expressions:

    For more information, see Specify Data Properties by Using MATLAB Expressions.

  4. To save the data type settings, click Apply.

The Data Type Assistant is available only through the Model Explorer.

Inherit Data Types from Simulink Objects

When you select Inherit: Same as Simulink from the Type drop-down list, data objects of scope Input, Output, Parameter, and Data Store Memory inherit their data types from Simulink objects.

ScopeDescription
InputInherits type from the Simulink input signal connected to corresponding input port in chart.
Output

Inherits type from the Simulink output signal connected to corresponding output port in chart.

Avoid inheriting data types from output signals. Values that back-propagate from Simulink blocks can be unpredictable.

ParameterInherits type from the corresponding MATLAB base workspace variable, Simulink data dictionary entry, or Simulink parameter in a masked subsystem.
Data Store MemoryInherits type from the corresponding Simulink data store.

To determine the data types that the objects inherit:

  1. Build the Simulink model.

  2. Open the Model Explorer.

  3. In the Contents pane, examine the CompiledType column.

Derive Data Types from Other Data Objects

You can use the type operator to derive data types from other Stateflow data objects. For example, the model sf_bus_demo uses the data type of the input structure inbus to define the data type of the local structure counterbus_struct by using this expression:

type(inbus)

Model Explorer showing the specification for the data object counterbus_struct.

Because inbus derives its type from the Simulink.Bus object COUNTERBUS, counterbus_struct also derives its data type from COUNTERBUS. After you compile your model, the CompiledType column of the Model Explorer shows the type used in the compiled simulation application.

For more information about this example, see Integrate Custom Structures in Stateflow Charts.

Specify Data Types by Using a Simulink Alias

You can specify the type of Stateflow data by using a Simulink data type alias. For more information, see Simulink.AliasType (Simulink).

For example, suppose that you want to define a data type alias MyFloat that corresponds to the built-in data type single. At the MATLAB command prompt, enter:

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

To use this alias to specify the type of a data object, select the object in the Property Inspector or the Model Explorer. In the Type field, enter the alias name MyFloat.

After you build your model, the CompiledType column of the Model Explorer shows the type used in the compiled simulation application.

Note

Stateflow blocks do not support code generation if one of the data uses an alias type and is variable size. This limitation does not apply to chart-level input, output, or local data. For more information on defining variable-size data, see Declare Variable-Size Data in Stateflow Charts.

See Also

(Simulink) | (Simulink) | (Simulink)

Related Topics