A state describes an operating mode of a reactive system. In a Stateflow® chart, states are used for sequential design to create state transition diagrams.
States can be active or inactive. The activity or inactivity of a state can change depending on events and conditions. The occurrence of an event drives the execution of the state transition diagram by making states become active or inactive. For more information, see States.
You create states by drawing them in the editor for a particular chart (block). Follow these steps:
Select the State tool:
Move your pointer into the drawing area.
In the drawing area, the pointer becomes state-shaped (rectangular with oval corners).
Click in a particular location to create a state.
The created state appears with a question mark (?)
label in its upper left-hand corner.
Click the question mark.
A text cursor appears in place of the question mark.
Enter a name for the state and click outside of the state when finished.
The label for a state specifies its required name and optional actions. See Label States for more detail.
To move a state, do the following:
Click and drag the state.
Release it in a new position.
To resize a state, do the following:
Place your pointer over a corner of the state.
When your pointer is over a corner, it appears as a double-ended arrow (PC only; pointer appearance varies with other platforms).
Click and drag the state's corner to resize the state and release the left mouse button.
A substate is a state that can be active only when another state, called its parent, is active. States that have substates are known as superstates. To create a substate, click the State tool and drag a new state into the state you want to be the superstate. A Stateflow chart creates the substate in the specified parent state. You can nest states in this way to any depth. To change the parentage of a substate, drag it from its current parent in the chart and drop it in its new parent.
Note
A parent state must be graphically large enough to accommodate all its substates. You might need to resize a parent state before dragging a new substate into it. You can bypass the need for a state of large graphical size by declaring a superstate to be a subchart. See Encapsulate Modal Logic by Using Subcharts for details.
Group a state to move all graphical objects inside a state together. When you group a state, the chart treats the state and its contents as a single graphical unit. This behavior simplifies editing of a chart. For example, moving a grouped state moves all substates and functions inside that state.
You can group a state by right-clicking it and then selecting Group & Subchart > Group in the context menu. The state appears shaded in gray to indicate that it is now grouped.
You must ungroup a state before performing these actions:
Selecting objects inside the state
Moving other graphical objects into the state
If you try to move objects such as states and graphical functions into a grouped state, you see an invalid intersection error message. Also, the objects with an invalid intersection have a red border.
You can ungroup a state by right-clicking it and then clearing Group & Subchart > Group in the context menu. The background of the state no longer appears gray.
You specify whether a superstate contains parallel (AND) states or exclusive (OR) states by setting its decomposition. A state whose substates are all active when it is active has parallel (AND) decomposition. A state in which only one substate is active when it is active has exclusive (OR) decomposition. An empty state's decomposition is exclusive.
To alter a state's decomposition, select the state, right-click to display the state's Decomposition context menu, and select OR (Exclusive) or AND (Parallel) from the menu.
You can also specify the state decomposition of a chart. In this case, the Stateflow chart treats its top-level states as substates. The chart creates states with exclusive decomposition. To specify a chart's decomposition, deselect any selected objects, right-click to display the chart's Decomposition context menu, and select OR (Exclusive) or AND (Parallel) from the menu.
The appearance of the substates indicates the decomposition of their superstate. Exclusive substates have solid borders, parallel substates, dashed borders. A parallel substate also contains a number in its upper right corner. The number indicates the activation order of the substate relative to its sibling substates.
You can specify activation order by using one of two methods: explicit or implicit ordering.
By default, when you create a new Stateflow chart, explicit ordering applies. In this case, you specify the activation order on a state-by-state basis.
You can also override explicit ordering by letting the chart order parallel states based on location. This mode is known as implicit ordering.
For more information, see Explicit Ordering of Parallel States and Implicit Ordering of Parallel States.
Note
The activation order of a parallel state appears in its upper right corner.
Use the State dialog box to view and change the properties for a state. To access the State dialog box:
Right-click the state and select Properties.
The State properties dialog box appears. For descriptions of properties, see Properties You Can Set in the General Pane and Properties You Can Set in the Logging Pane.
Modify property settings and then click one of these buttons:
Apply to save the changes and keep the State dialog box open
Cancel to return to the previous settings
OK to save the changes and close the dialog box
Help to display the documentation in an HTML browser window
The General pane of the State properties dialog box appears as shown.
You can set these properties in the General pane.
Property | Description |
---|---|
Stateflow chart name; read-only; click this hypertext link to bring the state to the foreground. | |
Set the execution order of a parallel (AND) state. This property does not appear for exclusive (OR) states. See Execution Order for Parallel States. | |
Select this option to create state activity data. See Monitor State Activity Through Active State Data. | |
Select one of these options to control the inlining of state functions in generated code:
| |
The label for the state, which includes the name of the state and its associated actions. See Label States. |
The Logging pane of the State properties dialog box appears as shown.
You can set these properties in the Logging pane.
Property | Description |
---|---|
Saves the self activity value to the MATLAB® workspace during simulation. | |
Designates the state as a test point that can be monitored with a floating scope during model simulation. You can also log test point values into MATLAB workspace objects. See Monitor Test Points in Stateflow Charts. | |
Specifies the name associated with the logged self
activity. Simulink® software uses the signal name as its logging
name by default. To specify a custom logging name, select
| |
Limits the self activity logged to the most recent samples. | |
Limits self activity logged by skipping samples. For example, a decimation factor of 2 saves every other sample. |
The Documentation pane of the State properties dialog box appears as shown.
You can set these properties in the Documentation pane.
The label for a state specifies its required name for the state and the optional actions executed when the state is entered, exited, or receives an event while it is active.
State labels have the following general format.
name/ entry:entry actions during:during actions exit:exit actions bind:data and events on event_or_message_name:on event_or_message_name actions
The italicized entries in this format have the following meanings:
Keyword | Entry | Description |
---|---|---|
Not applicable | name | A unique reference to the state with optional slash |
| entry actions | Actions executed when a particular state is entered as the result of a transition taken to that state |
| during actions | Actions that are executed when a state receives an event while it is active with no valid transition away from the state |
| exit actions | Actions executed when a state is exited as the result of a transition taken away from the state |
| data or events | Binds the specified data or events to this state. Bound data can be changed only by this state or its children, but can be read by other states. Bound events can be broadcast only by this state or its children. |
| event_or_message_name and on event_name actions | A specified event or message and Actions executed when a state is active and the specified event occurs or message is present. For more information, see Synchronize Model Components by Broadcasting Events and Communicate with Stateflow Charts by Sending Messages. |
Initially, a state's label is empty. The Stateflow chart indicates this by displaying a ? in the state's label position (upper left corner). Begin labeling the state by entering a name for the state with the following steps:
Click the state.
The state turns to its highlight color and a question mark character appears in the upper left-hand corner of the state.
Click the ? to edit the label.
An editing cursor appears. You are now free to type a label.
Enter the state's name in the first line of the state's label. Names are case sensitive. To avoid naming conflicts, do not assign the same name to sibling states. However, you can assign the same name to states that do not share the same parent.
After labeling the state, click outside it. Otherwise, continue entering actions. To reedit the label, click the label text near the character position you want to edit.
After entering the name of the state in the label, you can enter actions for any of the following action types:
Entry Actions — begin on a new
line with the keyword entry
or en
,
followed by a colon, followed by one or more action statements on one or
more lines. To separate multiple actions on the same line, use a comma
or a semicolon.
You can begin entry actions on the same line as the state's name. In
this case, begin the entry action with a forward slash
(/)
instead of the entry keyword.
Exit Actions — begin on a new
line with the keyword exit
or ex
,
followed by a colon, followed by one or more action statements on one or
more lines. To separate multiple actions on the same line, use a comma
or a semicolon.
During Actions — begin on a
new line with the keyword during
or
du
, followed by a colon, followed by one or more
action statements on one or more lines. To separate multiple actions on
the same line, use a comma or a semicolon.
Bind Actions — begin on a new
line with the keyword bind
followed by a colon,
followed by one or more data or events on one or more lines. To separate
multiple actions on the same line, use a comma or a semicolon.
On Actions — begin with the
keyword on
, followed by a space and the name of an
event or message, followed by a colon, followed by one or more action
statements on one or more lines, for example
on ev1: exit();
To separate multiple actions on the same line, use a comma or a
semicolon. If you want different events to trigger different actions,
enter multiple on
blocks in the state label. Each
block specifies the action for a specific event or message, for
example:
on ev1: action1(); on ev2: action2();
The execution of the actions you enter for a state is dependent only on their action type, and not the order in which you enter actions in the label. If you do not specify the action type explicitly for a statement, the chart treats that statement as an entry action.
Tip
You can also edit the label in the properties dialog box for the state. See Change State Properties.