| Contents | Index |
| On this page… |
|---|
Benefits of Using Enumerated Types in This Model How the UserRequest Chart Works |
This Simulink model implements a basic CD player using enumerated data in three Stateflow charts.

| Model Component | Description | Details |
|---|---|---|
| UserRequest chart | Reads and stores user inputs | How the UserRequest Chart Works |
| CdPlayerModeManager chart | Determines whether the CD player operates in CD or radio mode | How the CdPlayerModeManager Chart Works |
| CdPlayerBehaviorModel chart | Describes behavior of the CD player mechanism | How the CdPlayerBehaviorModel Chart Works |
This model uses two enumerated data types: RadioRequestMode and CdRequestMode.
| Enumerated Data Type | Enumerated Values |
|---|---|
| RadioRequestMode |
|
| CdRequestMode |
|
By grouping related values into separate data types, you get these benefits:
Enhance readability of data values in each chart.
Avoid defining a long list of constants, which reduces the amount of data in your model.
Follow these steps to run the model:
Type sf_cdplayer at the MATLAB command prompt.
Start simulation of the model.
The CD Player Helper GUI appears.

The Display blocks in the model show the default settings of the CD player.

In the CD Player Helper GUI, click CD in the Radio Request section.
The Display blocks for enumerated data RR and CurrentRadioMode change from OFF to CD.
In the CD Player Helper GUI, click Insert Disc.
The Display block for enumerated data CdStatus changes from EMPTY to DISCINSERT to STOP.
In the CD Player Helper GUI, click PLAY in the CD Request section.
The Display blocks for enumerated data CR, MechCmd, and CdStatus change from STOP to PLAY.
Note To see other changes in the Display blocks, you can select other operating modes for the CD player, such as FM or AM radio. |
Key features of the UserRequest chart include:
Enumerated data
ml namespace operator (see ml Namespace Operator)

This chart reads user inputs from the CD Player Helper GUI and stores the information as output data.
| Output Data Name | Data Type | Description |
|---|---|---|
| RR | Enumerated | Operating mode of the radio component |
| CR | Enumerated | Operating mode of the CD component |
| DiscInsert | Boolean | Setting for CD insertion |
| DiscEject | Boolean | Setting for CD ejection |
Key features of the CdPlayerModeManager chart include:
Enumerated data
Subcharts (see Using Subcharts to Encapsulate Modal Logic)
Change detection (see Detecting Changes in Data Values)

When the chart wakes up, the ModeManager state is entered.
The previously active substate recorded by the history junction becomes active: Standby or ON.
Note Transitions between the Standby and ON substates occur as follows.
|
If the Boolean data DiscEject is 1 (or true), a transition to the Eject state occurs, followed by a transition back to the ModeManager state.
Steps 2 and 3 repeat until the chart goes to sleep.
In the ON substate, three subcharts represent the operating modes of a CD player: CD, AM radio, and FM radio. Each subchart corresponds to a different value of enumerated data RadioReq.
| Value of Enumerated Data RadioReq | Active Subchart | Purpose of Subchart |
|---|---|---|
| CD | CDMode | Outputs play, rewind, fast forward, and stop commands to the CdPlayerBehaviorModel chart |
| AM | AMMode | Sets the CD player to AM radio mode |
| FM | FMMode | Sets the CD player to FM radio mode |
The hasChanged operator detects changes in the value of RadioReq with an inner transition.
Key features of the CdPlayerBehaviorModel chart include:
Enumerated data
Temporal logic (see Using Temporal Logic in State Actions and Transitions)

When the chart wakes up, the Empty state is entered.
If the Boolean data DiscInsert is 1 (or true), a transition to the Inserting state occurs.
After a short time delay, a transition to the DiscPresent state occurs.
The DiscPresent state remains active until the data CMD becomes EJECT.
If the enumerated data CMD is EJECT, a transition to the Ejecting state occurs.
After a short time delay, a transition to the Empty state occurs.
Steps 2 through 6 repeat until the chart goes to sleep.
Whenever a state transition occurs, the enumerated data CdStatus changes value to reflect the behavior of the CD player.
| Active State | Value of Enumerated Data CdStatus | Behavior of CD Player |
|---|---|---|
| Empty | EMPTY | CD player is empty. |
| Inserting | DISCINSERT | CD is being inserted into the player. |
| DiscPresent.STOP | STOP | CD is present and stopped. |
| DiscPresent.PLAY | PLAY | CD is present and playing. |
| DiscPresent.REW | REW | CD is present and rewinding. |
| DiscPresent.FF | FF | CD is present and fast forwarding. |
| Ejecting | EJECT | CD is being ejected from the player. |
![]() | Best Practices for Using Enumerated Data in a Chart | Tutorial: Using Enumerated Values for Assignment | ![]() |

Learn how engineers use Stateflow to model state machines in their Simulink models.
| © 1984-2012- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |