Working with States in Stateflow® Charts

Creating a State

You create states by drawing them in the Stateflow® Editor for a particular Stateflow chart (block). The following is a depiction of the Stateflow Editor:

  1. Select the State tool.

  2. Move your pointer into the drawing area.

    In the drawing area, the pointer becomes state-shaped (rectangular with oval corners).

  3. Click in a particular location to create a state.

    The created state appears with a question mark (?) label in its upper left-hand corner.

  4. Click the question mark.

    A text cursor appears in place of the question mark.

  5. 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 Labeling States for more detail.

To delete a state, click it to select it and choose Cut from the Edit or any shortcut menu or press the Delete key.

Moving and Resizing States

To move a state, do the following:

  1. Click and drag the state.

  2. Release it in a new position.

To resize a state, do the following:

  1. 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).

  2. Click and drag the state's corner to resize the state and release the left mouse button.

Creating Substates and Superstates

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 a substate's parentage, drag it from its current parent in the chart and drop it in its new parent.

Grouping States

Grouping a state causes a Stateflow chart to treat the state and its contents as a graphical unit. This simplifies editing a chart. For example, moving a grouped state moves all its substates as well.

To group a state, double-click the state or its border.

The grouped state thickens its border and grays its contents to indicate that it is grouped.

You can also group a state by right-clicking it and then selecting Make Contents and then Grouped from the resulting shortcut menu.

You must ungroup a superstate to select objects within the superstate. To ungroup a state, double-click it or its border or select Ungrouped from the Make Contents shortcut menu.

Specifying Substate Decomposition

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 shortcut menu, and choose either Parallel (AND) or Exclusive (OR) 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 shortcut menu, and choose either Parallel (AND) or Exclusive (OR) from the menu.

The appearance of a superstate's substates indicates the superstate's decomposition. 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.

Specifying Activation Order for Parallel States

You specify the activation order of parallel states by arranging them from top to bottom and left to right. A Stateflow chart activates the states in the order in which you arrange them. In particular, a top-level parallel state activates before all the states whose top edges reside at a lower level in the chart. A top-level parallel state also activates before any other state that resides to the right of it at the same vertical level in the chart. The same top to bottom, left to right activation order applies to parallel substates of a state.

Changing State Properties

Use the State dialog box to view and change the properties for a state. To access the State dialog for a particular state, do the following:

  1. Right-click the state.

    A shortcut pop-up menu appears.

  2. Choose Properties from the shortcut menu.

    The State dialog for the state appears as shown.

The State dialog contains the following properties for a state:

Field

Description

Name

Stateflow chart name; read-only; click this hypertext link to bring the state to the foreground.

Debugger breakpoints

Click the check boxes to set debugging breakpoints on the execution of state entry, during, or exit actions during simulation. See Debugging and Testing for more information.

Test point

Select this check box to set 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 Monitoring Test Points in Stateflow® Charts in the online Stateflow software documentation.

Output State Activity

Select this check box to cause the Stateflow chart to output the activity status of this state to a Simulink® model via a data output port on the Chart block containing the state. See Outputting State Activity to a Simulink® Model for more information.

Label

The label for the state. This includes the name of the state and its associated actions. See the section titled Labeling States for detailed information.

Description

Textual description/comment.

Document Link

Enter a URL address or a general MATLAB command. Examples are www.mathworks.com, mailto:email_address, and edit /spec/data/speed.txt.

  1. After making changes, select one of these options:

Labeling States

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_name:on event_name actions

The italicized entries in this format have the following meanings:

Keyword

Entry

Description

NA

name

A unique reference to the state with optional slash

entry or en

entry actions

Actions executed when a particular state is entered as the result of a transition taken to that state

during or du

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 or ex

exit actions

Actions executed when a state is exited as the result of a transition taken away from the state

bind

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.

on

event_name

and

on event_name actions

A specified event

and

Actions executed when a state is active and the specified event event_name occurs

See Adding Events for information on defining and using events.

Entering the Name

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:

  1. 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.

  2. 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.

    If you are finished labeling the state, click outside of the state. Otherwise, continue entering actions. To reedit the label, simply click the label text near the character position you want to edit.

Entering Actions

After entering the name of the state in the state's label, you can enter actions for any of the following action types:

You can also edit the state's label through the properties dialog for the state. See Changing State Properties.

Outputting State Activity to a Simulink® Model

You can output the activity of a chart's states to a Simulink model via a data port on the state's Chart block. To enable output of a particular state's activity, first name the state (see Entering the Name), if unnamed, and then select the Output State Activity check box on the state's property dialog (see Changing State Properties). A data output port appears on the Chart block containing the state. The port has the same name as the state. During simulation of a model, the port outputs 1 at each time step in which the state is active; 0, otherwise. Attaching a scope to the port allows you to monitor a state's activity visually during the simulation. See Sharing Input and Output Data with Simulink® Models for more information.

  


 © 1984-2008- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS