| Stateflow User's Guide | ![]() |
Using Junctions in Flow Diagrams
In Simulating Junction Behavior, you see the convenience of a junction in providing alternate paths to complete a transition between states. But you can use junctions without states to provide flow diagrams. Flow diagrams are "visible programming" that let you perform logical operations without using a state. In this section, you see a few examples of flow diagrams and how they can be useful to you.
The simplest flow diagram is a default transition into a junction. You have already seen this flow diagram in Adding a Graphical Function for Convenience. You use it to execute whatever actions you attach to the default transition. The following is an example of the simplest flow diagram:
When this diagram receives an update, the default transition is taken into the junction. Because there is no other flow out of the junction, it is a terminating junction. When Stateflow encounters a terminating junction, the chart is exited altogether. When the next update occurs, execution starts over again with the default transition. This is different from a state that stays active between updates.
The benefit of flow diagrams is their ability to perform complex logic in a visible environment. For example, the following flow diagram behaves like an if statement:
This flow diagram would have the following code representation:
To test the behavior of this flow diagram, do the following:
SFcontrol2.mdl.
SFcontrol_if.mdl.
SFcontrol2.mdl (and now SFcontrol_if.mdl) and initialized to zero by default.
SFcontrol_if.mdl.
| Because speed is no longer equal to 0, when the chart is updated again, the alternate path is taken. |
The Display block in Simulink reflects the resulting value for speed.
Besides the flow diagram for an if statement, there are a number of other flow diagram types. For example, you can add an action to the alternate path of the if diagram to create an if-else diagram.
| IF-ELSE |
Here are a few examples of other flow diagrams that you can try on your own. Don't forget to define the counter data i.
| FOR loop |
|
| WHILE |
| Introducing Stateflow Semantics | Controlling with Superstates | ![]() |
© 1994-2005 The MathWorks, Inc.