Create a Hierarchy to Manage System Complexity
To control multiple levels of complexity in your system, create a hierarchy of nested states in your Stateflow® chart by placing one or more states inside the boundaries of another state.
The outer state is the parent, or superstate, of the inner states. The inner states are child states, or substates, of the outer state. The contents of the superstate behave like a smaller chart. When the superstate becomes active, one of the substates also becomes active. When the superstate becomes inactive, all of the substates become inactive.
Model a Media Player
This example models a media system that consists of an FM radio and an internet stream player. During simulation, you control the media player by interacting with the buttons and knobs on the Media Player App user interface.
To run the media player:
Open the Simulink model and click Run. The Media Player App opens. The media player is initially off. At the top of the app, the Media Player Status box shows the message
Turn the Component Selection knob to Stream. The status message briefly displays
Connecting to Handel's Greatest Hits. After a short pause, the status message changes to
Playing: Handel's Greatest Hitsand music begins to play.
Click the Fast-Forward button. The music stops and chirping sounds begin. The status message changes to
Forward >> Handel's Greatest Hits. The name of the stream scrolls forward across the display. To resume normal play mode, click the Play button.
Click the Reverse button. Chirping sounds play and the status message changes to
Reverse >> Handel's Greatest Hits. The name of the stream scrolls backward across the display. To resume normal play mode, click the Play button.
In the Stream Name box, enter the name of a new stream and click Connect. For example, try connecting to the streams
Training Deep Networksor
Fun With State Machines.
Turn the Component Selection knob to Radio. The status message displays
Playing: 99.5 FM. To select another station, turn the FM Radio Station knob.
To stop the simulation, close the Media Player App.
Implement Behavior by Using State Hierarchy
This example implements the media player by focusing on a single level of activity at a time. For example, these conditions are necessary for the stream player to enter fast-forward playback mode:
You turn on the media player.
You select the stream player.
You start playing a stream.
You click the Fast-Forward button.
The model uses a hierarchy of nested states to consider each condition individually. For instance, the Model Explorer shows the hierarchy of states in the
Mode Manager chart. To open the Model Explorer, in the Modeling tab, select Model Explorer.
At the top or outermost level of the hierarchy, the
Mode Manager chart has two states that control the activity of the media player:
Offis active when you turn off the media player.
Onis active when you set the media player to streaming or radio mode.
The child states of
On control the media player components:
Streamis active when you select the stream player.
Radiois active when you select the FM radio.
The child states of
Stream control the activity of the stream player:
Playis active when you play a stream.
Pauseis active when you pause the stream player.
The child states of
Play control the play modes for the stream player:
Normalis active during normal play mode.
Reverseis active when you click the Reverse button.
FastForwardis active when you click the Fast-Forward button.
This figure shows the layout of the states in the chart.
Explore the Example
The model in this example contains two other Stateflow charts:
App Interfacemanages the interface with the MATLAB app and passes inputs to the
Stream Playerreceives the output from the
Mode Managercharts and simulates the internal behavior of the stream player.
During simulation, you can investigate how each chart responds to interactions with the Media Player App. To switch quickly between charts, use the tabs at the top of the Stateflow Editor.