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_cdplayer uses the hierarchy of nested states listed by the Model Explorer under the Media Player 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 (Media Player Mode Manager chart)ModeManagerNormal operating mode for stereo system
EjectDisc ejection mode (interrupts all other stereo functions)

Stereo system activity (child states of ModeManager)

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

Stereo subcomponents (child states of On)

AMModeAM radio subcomponent is active
FMModeFM radio subcomponent is active
CDModeCD player 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
RewReverse play mode
FastForwardFast-Forward play mode

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

Arrangement of states in the Media Player Mode Manager chart.

Simplify Chart Appearance by Using Subcharts

You can simplify the overall appearance of a chart with a complex hierarchy by hiding low-level details inside subcharts, which appear as opaque boxes. The use of subcharts does not change the behavior of the chart. For instance, in sf_cdplayer, the stereo subcomponents AMMode, FMMode, and CDMode are implemented as subcharts. When you open the Media Player Mode Manager chart, you see only three levels of the state hierarchy. To see the details inside one of the subcharts, double-click the subchart.

Subcharts appear as opaque boxes to hide the low-level details of the chart.

Explore the Example

The example sf_cdplayer contains two other Stateflow charts:

  • User Request manages the interface with the UI and passes inputs to the Media Player Mode Manager and CD Player Behavior Model charts.

  • CD Player Behavior Model receives the output from the User Request and Media Player 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. To switch quickly between charts, use the tabs at the top of the Stateflow Editor.

Related Examples

More About