Main Content

Create a Hierarchy to Manage System Complexity

Add structure to your model one subcomponent at a time by creating a hierarchy of nested states. You can then control multiple levels of complexity in your Stateflow® chart. For more information, see Model Finite State Machines.

State Hierarchy

To create a hierarchy of states, place one or more states within the boundaries of another state. The inner states are child states (or substates) of the outer state. The outer state is the parent (or superstate) of the inner states.

Stateflow chart with a hierarchy of states. The outer state is called Parent. It contains two inner states called Child1 and Child2.

The contents of a parent state behave like a smaller chart. When a parent state becomes active, one of its child states also becomes active. When the parent state becomes inactive, all of its child states become inactive.

Example of Hierarchy

This example models a stereo system consisting of an AM radio, an FM radio, and a CD player. During simulation, you control the stereo system by clicking buttons on the Media Player Helper user interface.

The stereo is initially in standby mode (OFF). When you select one of the Radio Request buttons, the stereo turns on the corresponding subcomponent. If you select the CD player, you can click one of the CD Request buttons to choose Play, Rewind, Fast-Forward, or Stop. You can insert or eject a disc at any point during the simulation.

Implement Behavior by Using State Hierarchy

Initially, the complete implementation of this stereo system appears rather complicated. However, by focusing on a single level of activity at a time, you can design the overall system design incrementally. For example, these conditions are necessary for the CD player to enter Fast-Forward play mode:

  1. You turn on the stereo.

  2. You turn on the CD player.

  3. You playing a disc.

  4. You click the FF button in the UI.

You can construct a hierarchical model that considers each of these conditions one at a time. For instance, the outermost level can define the transitions between the stereo turning on and off. The middle levels define the transition between the different stereo subcomponents, and between the stop and play modes of the CD player. The bottommost level defines the response to the CD Request buttons when you meet all the other conditions for playing a disc.

To implement the behavior of the stereo system, sf_mediaplayer uses the hierarchy of nested states listed by the Model Explorer under the Mode Manager chart. To open the Model Explorer, in the Modeling tab, select Model Explorer.

Hierarchy of nested states, as listed by the Model Explorer.

This table lists the role of each state in the hierarchy.

Hierarchy LevelStateDescription
Top level (Mode Manager chart)EjectDisc ejection mode (interrupts all other stereo functions)
NormalOperationNormal operating mode for stereo system

Stereo system activity (child states of NormalOperation)

StandbyStereo system is in standby mode (OFF)
ONStereo system is active (ON)

Stereo subcomponents (child states of On)

CDModeCD player subcomponent is active
AMModeAM radio subcomponent is active
FMModeFM radio subcomponent is active

CD player activity (child states of CDMode)

StopCD player is stopped
PlayCD player is playing disc

Disc play modes (child states of Play)

NormalNormal play mode
RewindReverse play mode
FastForwardFast-Forward play mode

This figure shows the complete layout of the states in the chart.

Arrangement of states in the Mode Manager chart.

The history junctions inside the NormalOperation and ON states record the operating mode (Standby or ON) and the active subcomponent of the media player (CD player, AM radio, or FM radio) before you eject a disc or turn off the system.

Explore the Example

The example sf_mediaplayer contains two other Stateflow charts:

  • App Interface manages the interface with the UI and passes inputs to the Mode Manager and CD Player charts.

  • CD Player receives the output from the App Interface and Mode Manager charts and mimics the mechanical behavior of the CD player.

Simulink model that simulates a media player. The model contains the chart Media Player Mode Manager and two additional charts, User Request and CD Player Behavior Model.

During simulation, you can investigate how each chart responds to interactions with the Media Player Helper app. To switch quickly between charts, use the tabs at the top of the Stateflow Editor.

Related Topics