Main Content

Specify Scope and Type of Stateflow Data

Data types define the format in which information is stored in computer memory. Data types influence three key aspects of your Stateflow® chart:

  • The amount of storage space assigned to data.

  • How a data value is converted into a binary pattern.

  • The set of operations that can be performed on the data.

Use data types to optimize the resource use of your charts and ensure that software applications correctly manipulate the chart data.

In Stateflow, you define data by their type, scope, and mode. The data type is the specific way data is stored in the system. For example, you could have a double or single data type. The scope is how the data operates in the Stateflow chart. For example, input data is data that Simulink® inputs into Stateflow, while local data is data that originates in and stays within Stateflow. The mode specifies the subset of type that you can choose. For example, if the mode is Inherited, then the data takes its information from Simulink, but if the mode is Expression, then the data information depends on an expression that you input. Mode can only be specified when using the Data Type Assistant.

You can specify the data type in the Property Inspector. 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.

Inherit Data Types from Simulink Objects

When you set the Type drop-down list to Inherit: Same as Simulink, data objects of scope Input, Output, Parameter, and Data Store Memory inherit their data types from Simulink objects. If you expect your data to be a specific type, do not set the Type as Inherit: Same as Simulink.

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

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

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. The structure is defined by using this expression:

type(inbus)

Property Inspector for sf_bus_demo.

Because inbus derives its type from the Simulink.Bus object COUNTERBUS, counterbus_struct also derives its data type from COUNTERBUS.

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 data type, open a Stateflow chart and, in the Symbols pane, select the data. Then, in the Property Inspector, set Type to MyFloat.

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

See Also

(Simulink) | (Simulink) | (Simulink)

Related Topics