Main Content

Types of Chart Execution

Life Cycle of a Stateflow Chart

Stateflow® charts go through several stages of execution:

StageDescription
InactiveChart has no active states
ActiveChart has active states
SleepingChart has active states, but no events to process

When a Simulink® model first triggers a Stateflow chart, the chart is inactive and has no active states. After the chart executes and completely processes its initial trigger event from the Simulink model, it transfers control back to the model and goes to sleep. At the next Simulink trigger event, the chart changes from the sleeping to active stage.

See Use Events to Execute Charts.

Execution of an Inactive Chart

When a chart is inactive and first triggered by an event from a Simulink model, it first executes its set of default flow charts (see Order of Execution for a Set of Flow Charts). If this action does not cause an entry into a state and the chart has parallel decomposition, then each parallel state becomes active (see Enter a Chart or State).

If executing the default flow paths does not cause state entry, a state inconsistency error occurs.

Execution of an Active Chart

After a chart has been triggered the first time by the Simulink model, it is an active chart. When the chart receives another event from the model, it executes again as an active chart. If the chart has no states, each execution is equivalent to initializing a chart. Otherwise, the active substates execute. Parallel states execute in the same order that they become active.

Execution of a Chart at Initialization

You can set a Stateflow chart to execute only when it receives an input event. By default, these charts remain inactive until they receive their first input event.

If you want your chart to activate as soon as the simulation starts, you can enable the Execute (enter) Chart At Initialization chart property. When you turn on this option, the state configuration of a chart initializes at time 0 instead of the first occurrence of an input event. The default transition paths of the chart execute during the model initialization phase at time 0, corresponding to the mdlInitializeConditions() (Simulink) phase for S-functions. For more information, see Execute (enter) chart at initialization.

Note

If an output of this chart connects to a SimEvents® block, do not select this check box. To learn more about using Stateflow charts and SimEvents blocks together in a model, see the SimEvents documentation.

Due to the transient nature of the initialization phase, do not perform certain actions in the default transition paths of the chart — and associated state entry actions — which execute at initialization. Follow these guidelines:

  • Do not access chart input data, because blocks connected to chart input ports might not have initialized their outputs yet.

  • Do not call exported graphical functions from other charts, because those charts might not have initialized yet.

  • Do not broadcast function-call output events, because the triggered subsystems might not have initialized yet.

To control the level of diagnostic action for invalid access to chart input data, open the Configuration Parameters dialog box and, in the Diagnostics > Stateflow pane, set the Invalid input data access in chart initialization parameter to none, warning, or error. The default setting is warning. For more information, see Invalid input data access in chart initialization (Simulink).

Charts that do not contain states ignore the Execute (enter) Chart At Initialization chart property.

Example of Execution at Initialization

In this example, a Pulse block triggers a chart once every two seconds. Every time the Pulse block triggers the chart, the chart executes a single step. The chart does not execute steps between triggers.

The chart contains the symbol output, which has an initial value of 0. When the entry action executes, it changes the value of output to 1. When the during action executes, it changes the value of output to 2.

When you disable the Execute (enter) Chart At Initialization chart property, the chart is inactive when the simulation starts.

At 2 seconds, the Pulse block triggers the chart. The chart enters the Set_Output state. The entry action executes, setting output to 1.

At 4 seconds, the Pulse block triggers the chart a second time. The chart stays in the Set_Output state. The during action executes, setting output to 2.

When you enable the Execute (enter) Chart At Initialization chart property, the chart takes one step as soon as the simulation starts. The chart enters the Set_Output state.

At 2 seconds, the Pulse block triggers the chart. The chart stays in the Set_Output state. The during action executes, setting output to 1.

Related Topics