This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

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.

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

The Stateflow example sf_cdplayer 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 CD Player Helper.

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.

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 turned on the stereo.

  2. You turned on the CD player.

  3. You are playing a disc.

  4. You clicked 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 if you have met 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 CdPlayerModeManager chart. To open the Model Explorer, select View > Model Explorer > Model Explorer.

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

Hierarchy LevelStateDescription
Top level (Stateflow chart CdPlayerModeManager)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.

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 chart CdPlayerModeManager, you see only three levels of the state hierarchy. To see the details inside one of the subcharts, double-click the subchart.

Explore the Example

The example sf_cdplayer contains two other Stateflow charts:

  • UserRequest manages the interface with the UI and passes inputs to the CdPlayerModeManager chart.

  • CdPlayerBehaviorModel receives the output from the CdPlayerModeManager and mimics the mechanical behavior of the CD player.

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

Related Examples

More About