Main Content

Use Events to Execute Charts

An event is a nongraphical object that can wake up and trigger actions in Stateflow® chart. For more information, see Synchronize Model Components by Broadcasting Events.

How Stateflow Charts Respond to Events

Stateflow charts respond to events in a cyclical manner.

  1. An event wakes up an inactive chart.

  2. The chart responds to the event by executing transitions and state actions from the top down through the chart hierarchy. Starting at the chart level:

    1. The chart checks for valid transitions between states.

    2. The chart executes during and on actions for the active state.

    3. The chart proceeds to the next level down the hierarchy.

  3. The chart becomes inactive until it receives the next event.

Flow chart that shows how a chart responds to an event.

For more information, see Execution of a Stateflow Chart.

Events in Simulink Models

In Simulink® models, Stateflow charts receive input events from other blocks in the model.

While processing an event, a state or transition action can generate explicit or implicit events that trigger additional steps. For example:

  • The operator send can broadcast local or output events.

  • The operators enter and exit can generate implicit local events when the chart execution enters or exits a state.

  • The operator change can generate an implicit local event when the chart sets the value of a variable.

In each case, the chart interrupts its current activity to process the new event. When the activity caused by the new event finishes executing, the chart returns to the activity that was taking place before the interruption.

Note

In a Simulink model, the execution of output edge-trigger events is equivalent to toggling the value of an output data value between 0 and 1. This type of event does not interrupt the current activity of a chart. Instead, the receiving block processes the event the next time that the model executes the block. For more information, see Activate a Simulink Block by Sending Output Events.

Early Return Logic

The results of processing a local event can conflict with the action that was taking place before the event was generated. Depending on the type of action, charts resolve these conflicts by using early return logic.

Action Type

Early Return Logic

State entry action

If the state is no longer active after the local event is processed, the chart stops the process of entering the state. The chart does not perform the remaining statements in the entry action.

If an event inside an entry action causes the chart to exit and reenter the same state that sent the event, the chart does not perform the remaining statements in the entry action. (since R2024a)

State during actionIf the state is no longer active after the local event is processed, the chart stops executing the state. The chart does not perform the remaining statements in the during action.
State exit actionIf the state is no longer active after the local event is processed, the chart stops the process of exiting the state. The chart does not perform the remaining statements in the exit action nor any transition actions and state entry actions that result from exiting the state.
Condition actionIf the source state of the inner or outer transition path, or the parent state of the default transition path, is no longer active after the local event is processed, the chart stops the transition process. The chart does not perform the remaining actions on the transition path or any state exit and entry actions that result from taking the transition.
Transition actionIf the parent of the transition path is not active, or if the parent has an active substate, the chart stops the transition process. The chart does not perform the remaining actions on the transition path or any state entry actions that result from taking the transition.

For example, in this chart, the input event E and the local event F trigger the transitions between states.

Stateflow chart with states called A, B, C, and D.

Suppose that state A is active when the chart receives event E. The chart responds to the event with these steps:

  1. The chart determines that the transition from the active state A to state B is valid as a result of event E.

  2. The chart executes the condition action of the valid transition and broadcasts event F.

  3. The chart interrupts the transition from state A to state B and begins to process event F.

  4. The chart determines that the transition from the active state A to state C is valid as a result of event F.

  5. State A executes its exit action.

  6. State A becomes inactive.

  7. State C becomes active.

  8. State C executes its entry action.

After the chart processes event F, state C is the active state of the chart. Because state A is no longer active, the chart uses early return logic and stops the transition from state A to state B.

Tip

Avoid using undirected local event broadcasts. Undirected local event broadcasts can cause unwanted recursive behavior in your chart. Instead, send local events by using directed broadcasts. For more information, see Broadcast Local Events to Synchronize Parallel States.

During simulation, Stateflow charts can detect undirected local event broadcasts. To control the level of diagnostic action, open the Configuration Parameters dialog box and, in the Diagnostics > Stateflow pane, set the Undirected event broadcasts parameter to none, warning, or error. The default setting is warning. For more information, see Undirected event broadcasts (Simulink).

Events in Standalone Charts

Standalone Stateflow charts receive an input event when you call the step function or an input event function in MATLAB®.

Standalone charts also receive implicit events from MATLAB timer objects associated with the absolute-time temporal logic operators after, at, and every. These operators define temporal logic in terms of wall-clock time. If the state associated with the temporal logic operator becomes inactive before the chart processes the implicit event, the event does not wake up the chart.

Queuing of Events

If a chart is processing another operation when it receives an event, the chart queues the event for execution when the current step is completed. You can specify the size of the event queue by setting the configuration option -eventQueueSize when you create the chart object. For more information, see Chart Object Configuration Options.

For example, in this chart, the input events E and F trigger the transitions between states. Assume that SendF is a function in the MATLAB path that calls the input event function F.

Stateflow chart with states called A, B, C, and D.

Suppose that state A is active when the chart receives event E. The chart responds to the event with these steps:

  1. The chart determines that the transition from the active state A to state B is valid as a result of event E.

  2. The chart executes the condition action of the valid transition and calls the function SendF.

  3. SendF calls the input event function F. Because the chart is busy processing a condition action, it queues event F.

  4. The chart completes executing the condition action.

  5. State A executes its exit action.

  6. State A becomes inactive.

  7. State B becomes active.

  8. State B executes its entry action.

  9. The chart begins to process the queued event F.

  10. The chart determines that the transition from the active state B to state D is valid as a result of event F.

  11. State B executes its exit action.

  12. State B becomes inactive.

  13. State D becomes active.

  14. State D executes its entry action.

After the chart processes event F, state D is the active state of the chart.

See Also

| | | | | | |

Related Topics