| Contents | Index |
| On this page… |
|---|
How Checking State Activity Works |
Check state activity when you have substates in parallel states that can be active at the same time. For example, checking state activity allows you to synchronize substates in two parallel states.
Use the in operator to check if a state is active. You can use this operator in state actions and transitions that originate from states.
Checks if a state is active in a given time step during chart execution.
in(S)
where S is a fully qualified state.
The in operator is true and returns a value of 1 whenever state S is active; otherwise, it returns a value of 0.
This example illustrates the use of the in operator in transition conditions.
![]()
In this chart, using the in operator to check state activity synchronizes substates in the parallel states Place and Tracker. For example, when the input position u becomes positive, the state transition from Place.L to Place.R occurs. This transition makes the condition [in(Place.R)] true, and the transition from Tracker.Moved_Left to Tracker.Moved_Right occurs.
Checking state activity is a two-stage process. First, the in operator must find the desired state. Then, the operator determines if the desired state is active.
The in operator does not perform an exhaustive search for all states in a chart that can match the argument. It performs a localized search and stops.
The in operator does not stop searching after it finds one match. It continues to search until it reaches the chart level.
This diagram shows the detailed process of checking state activity.

When you use the in operator to check state activity, these actions take place:
The search begins in the state where you use the in operator.
If you use the in operator in a state action, then that state is the starting point.
If you use the in operator in a transition label, then the parent of the source state is the starting point.
The in operator searches at that level of the hierarchy for a path to a state that matches the desired state. If the operator finds a match, it adds that state to the list of possible matches.
The operator moves up to the next highest level of the hierarchy. At that level, the operator searches for a path to a state that matches the desired state. If the operator finds a match, it adds that state to the list of possible matches.
The in operator repeats the previous step until it reaches the chart level.
At the chart level, the operator searches for a path to a state that matches the desired state. If the operator finds a match, it adds that state to the list of possible matches. Then, the search ends.
After the search ends, one of the following occurs:
If a unique search result is found, the in operator checks if that state is active and returns a value of 0 or 1.
If the operator finds no matches or multiple matches for the desired state, a warning message appears.
For identically named substates in parallel superstates, the scope of the in operator remains local with respect to its chart-level superstate. When the in operator checks activity of a substate, it does not automatically detect an identically named substate that resides in a parallel superstate.
This example shows how the in operator works in a chart with identically named substates.

Superstates A and B have identical substates A1 and A2.
The condition in(A1.Y) guards the transition from P to Q in the states A.A2 and B.A2.
For the state A.A2, the condition in(A1.Y) refers to the state A.A1.Y.
For the state B.A2, the condition in(A1.Y) refers to the state B.A1.Y.
For the transition condition of A.A2, the in operator performs these search actions:
| Step | Action of the in Operator | Finds a Match? |
|---|---|---|
| 1 | Picks A.A2 as the starting point and searches for the state A.A2.A1.Y | No |
| 2 | Moves up to the next level of the hierarchy and searches for the state A.A1.Y | Yes |
| 3 | Moves up to the chart level and searches for the state A1.Y | No |
The search ends, with the single state A.A1.Y found. The in operator checks if that state is active and returns a value of 0 or 1.
Localizing the scope of the in operator produces a unique search result. For example, the in operator of A.A2 does not detect the state B.A1.Y, because the search algorithm localizes the scope of the operator. Similarly, the in operator of B.A2 detects only the state B.A1.Y and does not detect the state A.A1.Y.
Be specific when defining the path of the state whose activity you want to check. See the examples that follow for details.
Example of No States Matching the Argument of the in Operator.

In the state A.B, the during action invokes the in operator. Assume that you want to check the activity of the state A.B.Other.C.D. The in operator performs these search actions:
| Step | Action of the in Operator | Finds a Match? |
|---|---|---|
| 1 | Picks A.B as the starting point and searches for the state A.B.C.D | No |
| 2 | Moves up to the next level of the hierarchy and searches for the state A.C.D | No |
| 3 | Moves up to the chart level and searches for the state C.D | No |
The search ends, and a warning message appears because no match exists.
To eliminate the warning message, use a more specific path to check state activity: in(Other.C.D).
Example of the Wrong State Matching the Argument of the in Operator.

In the state A.B, the during action invokes the in operator. Assume that you want to check the activity of the state A.B.Other.Q.R. The in operator performs these search actions:
| Step | Action of the in Operator | Finds a Match? |
|---|---|---|
| 1 | Picks A.B as the starting point and searches for the state A.B.Q.R | No |
| 2 | Moves up to the next level of the hierarchy and searches for the state A.Q.R | No |
| 3 | Moves up to the chart level and searches for the state Q.R | Yes |
The search ends, with the single state Q.R found. The in operator checks if that state is active and returns a value of 0 or 1.
In this example, the in operator checks the status of the wrong state. To prevent this error, use a more specific path to check state activity: in(Other.Q.R).
Use unique names when you name the states in a chart.
Example of Multiple States Matching the Argument of the in Operator.

In the state A.B, the during action invokes the in operator. Assume that you want to check the activity of the state A.B.P.Q.R. The in operator performs these search actions:
| Step | Action of the in Operator | Finds a Match? |
|---|---|---|
| 1 | Picks A.B as the starting point and searches for the state A.B.P.Q.R | Yes |
| 2 | Moves up to the next level of the hierarchy and searches for the state A.P.Q.R | No |
| 3 | Moves up to the chart level and searches for the state P.Q.R | Yes |
The search ends, and a warning message appears because multiple matches exist.
To eliminate the warning message, do one of these corrective actions:
Rename one of the matching states.
Use a more specific path to the desired state: in(B.P.Q.R).
Enclose the outer state P.Q.R in a box or another state, as shown below.
Adding an enclosure prevents the in operator of state A.B from detecting that outer state.

![]() | Detecting Changes in Data Values | Using Bind Actions to Control Function-Call Subsystems | ![]() |

Learn how engineers use Stateflow to model state machines in their Simulink models.
| © 1984-2012- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |