| Stateflow User's Guide | ![]() |
Using Stateflow Logic with Binding
You can use Stateflow logic to control function-call subsytems to compose C-like switch, if-else, for, and while statements in Simulink. For example, the following Simulink model demonstrates a Simulink switch statement with subsytems controlled by bind actions:
The preceding Stateflow diagram controls three subsystems, S1, S2, and S3, through the bind actions for three states, A, B, and C, respectively. In this example, the value of the case argument c is used to determine the subsystem to execute. State A becomes active and stays active when c is 0. State B becomes active and stays active when c is 1. State C becomes active and stays active when c has any other value.
When state A is active, the event S1 is bound to state A, which enables subsystem S1. The entry and during actions for A broadcast the event S1 whenever the model is updated for sampling. This means that while A is active, the subsystem S1 is executed for each sample time. The same applies to subsystem S2 for state B, and to subystem S3 for state C. This creates the following if-else statement in Simulink:
This creates the following switch statement in Simulink:
You can modify the previous Stateflow diagram to control a Simulink model with an if-else statement, as shown.
In this example, State A becomes active and stays active when the condition Acon is true. State B becomes active and stays active when the condition Bcon is true and the condition Acon is false. State C becomes active and stays active when both conditions Acon and Bcon are false. This creates the following if-else statement in Simulink:
| Simulating a Bound Function-Call Subsystem | Avoiding Muxed Trigger Events with Binding | ![]() |
© 1994-2005 The MathWorks, Inc.