This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

Check State Activity by Using the in Operator

In a Stateflow® chart with parallel state decomposition, substates can be active at the same time. If you check state activity, you can synchronize substates in two parallel states.

For example, this chart has two parallel states: Place and Tracker. The transitions in Tracker check the state activity in Place and keep the substates synchronized. A change of active substate in Place causes a corresponding change of active substate in Tracker.

  • If R becomes the active substate in Place, then Moved_Right becomes the active substate in Tracker.

  • If L becomes the active substate in Place, then Moved_Left becomes the active substate in Tracker.

The in Operator

To check if a state is active in a given time step during chart execution, use the in operator:

in(S)
The in operator takes a qualified state name S and returns a Boolean output. If state S is active, in returns a value of 1. Otherwise, in returns a value of 0.

You can use the in operator in state actions and in transitions that originate from states.

Resolution of State Activity

Checking state activity is a two-part process. First, Stateflow resolves the qualified state name by performing a localized search of the chart hierarchy for a matching state. Then, Stateflow determines if the matching state is active.

The search begins at the hierarchy level where the qualified state name appears:

  • For a state action, the starting point is the state containing the action.

  • For a transition label, the starting point is the parent of the transition source.

The resolution process searches each level of the chart hierarchy for a path to the state. If a state matches the path, the process adds that state to the list of possible matches. Then, the process continues the search one level higher in the hierarchy. The resolution process stops after it searches the chart level of the hierarchy. If a unique match exists, the in operator checks if the matching state is active. Otherwise, the resolution process fails. Simulation stops and you see an error message.

This flow chart illustrates the different stages in the process for checking state activity.

Best Practices for Checking State Activity

Resolving state activity:

  • Does not perform an exhaustive search for all states in a chart.

  • Does not stop after finding the first match.

To improve the chances of finding a unique search result when resolving qualified data names:

  • Use specific paths in qualified data names.

  • Give states unique names.

  • Use states and boxes as enclosures to limit the scope of the path resolution search.

Examples of State Activity Resolution

Search Finds Local Copy of Substate

This chart contains parallel states A and B that have identical substates A1 and A2. The condition in(A1.Y) guards the transition from P to Q in A.A2 and in B.A2. Stateflow resolves each qualified state name as the local copy of the substate Y:

  • In the state A, the condition in(A1.Y) checks the activity of state A.A1.Y.

  • In the state B, the condition in(A1.Y) checks the activity of state B.A1.Y.

This table lists the different stages in the resolution process for the transition condition in state A.A2.

Stage DescriptionResult
1Starting in state A.A2, search for the state A.A2.A1.Y.No match found.
2Move up to the next level of the hierarchy (state A). Search for the state A.A1.YMatch found.
3Move up to the next level of the hierarchy (the chart level). Search for the state A1.YNo match found.

The search ends with a single match found. Because the resolution algorithm localizes the scope of the search, the in operator guarding the transition in A.A2 detects only the state A.A1.Y. The in operator guarding the transition in B.A2 detects only the state B.A1.Y.

To check the state activity of the other copy of Y, use more specific qualified state names:

  • In state A, use the expression in(B.A1.Y).

  • In state B, use the expression in(A.A1.Y).

Search Produces No Matches

In this chart, the during action in state A.B contains the expression in(Q.R). Stateflow cannot resolve the qualified state name Q.R.

This table lists the different stages in the resolution process.

StageDescriptionResult
1Starting in state A.B, search for the state A.B.Q.R.No match found.
2Move up to the next level of the hierarchy (state A). Search for the state A.Q.R.No match found.
3Move up to the next level of the hierarchy (the chart level). Search for the state Q.R.No match found.

The search ends at the chart level with no match found for Q.R, resulting in an error.

To avoid this error, use a more specific qualified state name. For instance, check state activity by using the expression in(P.Q.R).

Search Finds the Wrong State

In this chart, the during action in state A.B contains the expression in(Q.R). When resolving the qualified state name Q.R, Stateflow cannot detect the substate A.B.P.Q.R.

This table lists the different stages in the resolution process.

StageDescriptionResult
1Starting in state A.B, search for the state A.B.Q.R.No match found
2Move up to the next level of the hierarchy (state A). Search for the state A.Q.R.No match found.
3Move up to the next level of the hierarchy (the chart level). Search for the state Q.R.Match found.

The search ends with a single match found. The in operator detects only the substate R of the top-level state Q.

To check the state activity of A.B.P.Q.R, use a more specific qualified state name. For instance, use the expression in(P.Q.R).

Search Produces Multiple Matches

In this chart, the during action in state A.B contains the expression in(P.Q.R). Stateflow cannot resolve the qualified state name P.Q.R.

This table lists the different stages in the resolution process.

StageDescriptionResult
1Starting in state A.B, search for the state A.B.P.Q.R.Match found
2Move up to the next level of the hierarchy (state A). Search for the state A.P.Q.R.No match found.
3Move up to the next level of the hierarchy (the chart level). Search for the state P.Q.R.Match found.

The search ends at the chart level with two matches found for P.Q.R, resulting in an error.

To avoid this error:

  • Use a more specific qualified state name. For instance:

    • To check the substate activity inside B, use the expression in(B.P.Q.R).

    • To check the substate activity in the top-level state P, use the expression in(\P.Q.R).

  • Rename one of the matching states.

  • Enclose the top-level state P in a box or another state. Adding an enclosure prevents the search process from detecting substates in the top-level state.

Related Topics