Stateflow | ![]() ![]() |
Labeling a State
The ? character is the default state label. State labels have this general format:
name/
entry: during: exit: onevent_name
:
The keywords entry
(shorthand en
), during
(shorthand du
), exit
(shorthand ex
), and on
identify actions associated with the state. You can specify multiple actions by separating them by any of these:
Specify multiple on
event_name
actions for different events by adding multiple on
event_name
lines specifying unique values for event_name
.
Each keyword is optional and positionally independent. You can specify none, some, or all of them. The colon after each keyword is required. The slash following the state name is optional as long as it is followed by a carriage return.
If you enter the name and slash followed directly by an action or actions (without the entry
keyword), the action(s) is interpreted as entry
action(s). This shorthand is useful if you are only specifying entry
actions.
See What Is an Action Language? for more information on the action language.
Example: Labeling a State
This example shows the state labeling formats and explains the components of the label.
Name. The name of the state forms the first part of the state label. Valid state names consist of alphanumeric characters and can include the _ character, e.g., Transmission
or Green_on
.
The use of hierarchy provides some flexibility in the naming of states. The name that you enter as part of the label must be unique when preceded by the hierarchy of its ancestor states. The name as stored in the data dictionary consists of the text you entered as the label on the state, preceded by the hierarchy of its ancestor states separated by periods. States can have the same name appear on the graphical representation of the state, as long as the full names within the data dictionary are unique. The parser indicates an error if a state does not have a unique name entry in the data dictionary for that Stateflow diagram.
See Example: Unique State Names for an example of uniquely named states.
In this example, the state names are On
and Off
.
Entry Action. In the example, state On
has entry
action on_count=0
. The value of on_count
is reset to 0 whenever state On
's entry
action is executed.
See Semantics of State Actions for information on how and when entry
actions are executed.
During Action. In the example, state On
has two during
actions light_on()
and on_count++.
These actions are executed whenever state On
's during
action is executed.
See Semantics of State Actions for information on how and when during
actions are executed.
Exit Action. In the example, state Off
has exit
action light_off()
. This action is executed whenever state Off
's exit
action is executed.
See Semantics of State Actions for information on how and when exit
actions are taken.
On Event_name Action. In the example, state Off
has the on event_name
, power_outage
. When the event power_outage
occurs, the action handle_outage()
is executed.
See Semantics of State Actions for information on how and when on
event_name
actions are taken.
Example: Unique State Names
This example shows how hierarchy supports unique naming of states.
Each of these states has a unique name because of the hierarchy of the Stateflow diagram. Although the name portion of the label on the state itself is not unique, when the hierarchy is prepended to the name in the data dictionary, the result is unique. The full names for the states as seen in the data dictionary are:
Ride1.On
Ride1.Off
Ride2.On
Ride2.Off
Although the names On
and Off
are duplicated, the full names are unique because of the hierarchy of the Stateflow diagram. The example intentionally contains only states for simplification purposes.
![]() | Combination States | Transitions | ![]() |