Main Content

Synchronize Model Components by Using Events

An event is a Stateflow® object that can trigger actions in:

  • A parallel state in a Stateflow chart

  • Another Stateflow chart

  • A Simulink® triggered or function-call subsystem

There is no limit to the number of events a Stateflow chart can contain. However, for code generation, the underlying C compiler enforces a limit of 231-1 events.

Types of Events

An implicit event is a built-in event that is broadcast during chart execution. These events are implicit because you do not define or trigger them explicitly. For more information, see Control Chart Behavior by Using Implicit Events.

An explicit event is an event that you define explicitly. Explicit events can be one of these types.

TypeDescription
Input EventAn event that is broadcast to a Stateflow chart from outside the chart. For more information, see Activate Stateflow Charts by Sending Input Events and Design Human-Machine Interface Logic by Using Stateflow Charts.
Local EventAn event that can occur anywhere in a Stateflow chart but is visible only in the parent object and its descendants. For more information, see Broadcast Local Events to Synchronize Parallel States.
Output EventAn event that occurs in a Stateflow chart but is broadcast to a Simulink block. For more information, see Activate a Simulink Block by Sending Output Events.

You can define local events at these levels of the Stateflow hierarchy:

Level of HierarchyVisibility
ChartLocal event is visible to the chart and its states and substates.
SubchartLocal event is visible to the subchart and its states and substates.
StateLocal event is visible to the state and its substates.

Define Events in a Chart

You can add events to a Stateflow chart by using the Symbols pane or the Stateflow Editor menu.

Add Events by Using the Symbols Pane

  1. In the Modeling tab, under Design Data, click Symbols Pane.

  2. Click the Create Event icon .

  3. In the row for the new event, under Type, click the icon and choose:

    • Input Event

    • Local Event

    • Output Event

  4. Edit the name of the event.

  5. For input and output events, click the Port field and choose a port number.

  6. To specify properties for the event, open the Property Inspector. In the Symbols pane, right-click the row for the event and select Inspect. For more information, see Set Properties for an Event.

Add Events by Using the Stateflow Editor Menu

  1. In a Stateflow chart in a Simulink model, select the menu option corresponding to the type of the event that you want to add.

    TypeMenu Option
    Input EventIn the Modeling tab, under Design Data, click Event Input.
    Output EventIn the Modeling tab, under Design Data, click Event Output.
    Local EventIn the Modeling tab, under Design Data, click Local Event.

  2. In the Event dialog box, specify the data properties. For more information, see Set Properties for an Event.

Access Event Information from a Stateflow Chart

You can display the properties of an input or local event, or open the destination of an output event directly from a Stateflow chart. Right-click the state or transition that contains the event and select Explore. A context menu lists the names and scopes of all resolved symbols in the state or transition. Selecting an input or local event from the context menu displays its properties in the Model Explorer. Selecting an output event from the context menu opens the Simulink subsystem or Stateflow chart associated with the event.

Best Practices for Using Events in Stateflow Charts

Follow these best practices when using events in charts.

Use the send Command to Broadcast Explicit Events in Actions

To broadcast local or output events in state or transition actions, use the send operator. For example, to broadcast an output event when a transition is valid, avoid using the name of the event as a condition action.

{output_event;}

Instead, call the send operator.

{send(output_event);}

Avoid Using Explicit Events to Trigger Conditional Actions

Use conditions on transitions instead of events when you want to:

  • Represent conditional statements, for example, [x < 1] or [x == 0].

  • Represent a change of data value, for example, [hasChanged(x)]

Avoid Using the Implicit Event enter to Check State Activity

To check state activity, use the in operator instead of the implicit event enter. For more information, see Check State Activity by Using the in Operator.

Do Not Mix Edge-Triggered and Function-Call Input Events in a Chart

Mixing input events that use edge triggers and function calls results in a compile-time error.

See Also

| |

Topics