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.
| Type | Description |
|---|---|
| Input Event | An 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 Event | An 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 Event | An 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 Hierarchy | Visibility |
|---|---|
| Chart | Local event is visible to the chart and its states and substates. |
| Subchart | Local event is visible to the subchart and its states and substates. |
| State | Local 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
In the Modeling tab, under Design Data, click Symbols Pane.
Click the Create Event icon
.In the row for the new event, under Type, click the icon and choose:
Input EventLocal EventOutput Event
Edit the name of the event.
For input and output events, click the Port field and choose a port number.
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
In a Stateflow chart in a Simulink model, select the menu option corresponding to the type of the event that you want to add.
Type Menu Option Input Event In the Modeling tab, under Design Data, click Event Input. Output Event In the Modeling tab, under Design Data, click Event Output. Local Event In the Modeling tab, under Design Data, click Local Event. 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.