Main Content

Monitor State Activity Through Active State Data

Active state data can simplify the design of some Stateflow® charts because you do not have to maintain data that is highly correlated to the chart hierarchy. When you enable active state data, Stateflow reports state activity through an output port to Simulink® or as local data in your chart. Using active state data, you can:

  • Avoid manual data updates reflecting chart activity.

  • View chart activity by using a scope, the Simulation Data Inspector, or the Logic Analyzer.

  • Log chart activity for diagnostics.

  • Drive other Simulink subsystems.

Types of Active State Data

When you enable active state data, Stateflow creates a Boolean or enumerated data object to match the activity type.

Activity TypeActive State Data TypeDescription
Self activityBooleanIs the state active?
Child activityEnumerationWhich child is active?
Leaf state activityEnumerationWhich leaf state is active?

For self-activity of a chart or state, the data value is true when active and false when inactive. For child and leaf state activity, the data is an enumerated type. Stateflow can define the enumeration class or you can create the definition manually. For more information, see Define State Activity Enumeration Type.

You can enable active state data for a Stateflow chart, state, state transition table, or atomic subchart. This table lists the activity types supported by each kind of Stateflow object.

Stateflow ObjectSelf-Activity Child ActivityLeaf State Activity
Charts Not supportedSupportedSupported
States with exclusive (OR) decompositionSupportedSupportedSupported
States with parallel (AND) decompositionSupportedNot supportedNot supported
Atomic subchartsSupported at the container levelSupported inside the subchartSupported inside the subchart
State transition tablesNot supportedSupportedSupported

Enable Active State Data

You can enable active state data in the Property Inspector, the Model Explorer, or the properties dialog box for the Stateflow object to monitor.

To use the Property Inspector:

  1. In the Modeling tab, under Design Data, select Property Inspector.

  2. In the Stateflow Editor, select the Stateflow object to monitor.

  3. In the Monitoring section of the Property Inspector, select Create output for monitoring and edit the active state data properties listed below.

To use the Model Explorer:

  1. In the Modeling tab, under Design Data, select Model Explorer.

  2. In the Model Hierarchy pane, double-click the Stateflow object to monitor.

  3. In the Dialog pane, select Create output for monitoring and edit the active state data properties listed below.

To use the properties dialog box:

  1. In the Stateflow Editor, right-click the Stateflow object to monitor.

  2. Select Properties.

  3. In the properties dialog box, select Create output for monitoring and edit the active state data properties listed below.

Activity Type

Type of state activity to monitor. Choose from these options:

  • Self activity

  • Child activity

  • Leaf state activity

Data Name

Name of the active state data object. For more information, see Guidelines for Naming Stateflow Objects.

Enum Name

Name of the enumerated data type for the active state data object. This property applies only to child and leaf state activity.

Define Enumerated Type Manually

Specifies whether you define the enumerated data type manually. This property applies only to child and leaf state activity. For more information, see Define State Activity Enumeration Type.

Set Scope for Active State Data

By default, active state data has a scope of Output. Stateflow creates an output port on the chart block in the Simulink model.

To access active state data inside a Stateflow chart, in the Symbols pane or in the Model Explorer, change the scope to Local. For more information, see Set Data Properties.

You can specify information for code generation by binding the local active state data to a Simulink.Signal (Simulink) object. Modify the properties of the object through the CoderInfo property. For more information, see Simulink.CoderInfo (Simulink).

Define State Activity Enumeration Type

By default, Stateflow defines the enumeration data type for child and leaf activity. If you select the Define enumerated type manually check box and no enumeration data type definition exists, then Stateflow provides a link to create a definition. Clicking the Create enum definition from template link generates a customizable definition.

Logging properties for state gear_state as seen in Property Inspector.

The enumeration data type definition contains one enumerated value for each state name. By default, the definition contains an enumerated value that serves as the default value for the enumeration data type and indicates that no substate is active. For example, in the model sf_car, the state gear_state contains four child states that correspond to the gears in a car: first, second, third, fourth. To open this example, enter:

openExample("stateflow/AutomaticTransmissionWithActiveStateDataExample")

The model specifies the child activity data type with this enumeration class definition:

classdef gearType < Simulink.IntEnumType 
   enumeration
		None(0),
		first(1),
		second(2),
		third(3),
		fourth(4)
   end
...
end

You can customize this definition by reordering the enumerated values or renaming the default value. Do not rename the enumerated values that correspond to states or use the getDefaultValue method to specify a different default value. For more information, see Define Enumerated Data Types.

Tip

The base storage type for automatically created enumerations defaults to Native Integer. To use a smaller memory footprint, open the Configuration Parameters dialog box and, in the Optimization pane, change the value of the Base storage type for automatically created enumerations parameter. For more information, see Base storage type for automatically created enumerations (Simulink Coder).

State Activity and Parallel States

In states with parallel (AND) decomposition, child activity and leaf state activity are not available because the parallel substates are active simultaneously.

When you enable leaf state activity in a chart or state, a substate with parallel (AND) decomposition is treated as a leaf state. For example, suppose that you enable leaf state activity for this chart. Because state B has parallel decomposition, its substates B1 and B2 are active simultaneously so B is treated as a leaf state of the chart.

Chart with two superstates called A and B. State B has two parallel substates called B1 and B2.

During simulation, a scope connected to the active state output data shows the enumerated values for the leaf states A1, A2, and B.

Scope block that shows active leaf states during simulation.

Limitations for Active State Data

  • Enabling child activity output for states that have no children results in an error at compilation and run time.

  • You cannot enable child or leaf state activity in charts or states with parallel decomposition. To check state activity in substates of parallel states, use the in operator. For more information, see Check State Activity by Using the in Operator.

  • Active state data is not supported in charts that use classic or Mealy semantics when the chart property Initialize outputs every time chart wakes up is enabled. For more information, see Initialize outputs every time chart wakes up.

See Also

Objects

Tools

Related Topics