Simulate a Media Player
This example shows how to create an interface between a Stateflow® chart that uses C as the action language and a MATLAB® app created in App Designer. For more information on connecting a Stateflow chart that uses MATLAB as the action language to a MATLAB app, see Model a Power Window Controller.
In this example, a MATLAB app models the front end of a media player. During simulation, you can choose between an FM radio and an internet stream player.

A Simulink® model defines the logic for the media player. The model contains these Stateflow charts:
App Interfaceprovides a bidirectional connection between the MATLAB app and the other charts in the Simulink® model.Mode Managerdetermines whether the media player is on or off, whether the FM radio or stream player is active, and the whether the stream player is operating in normal, reverse, or fast-forward playback mode.Stream Playersimulates the internal behavior of the stream player component.
When you interact with the buttons and knobs in the app, App Interface sends a corresponding command to the other charts in the model. These charts use enumerated data to control the behavior of the media player and string data to provide natural language output messages that indicate the status of the media player. When the state of the media player changes, App Interface updates the status message and changes the appearance of the buttons in the app.

Run the Media Player Model
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
Standby (Off).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 toPlaying: 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 NetworksorFun 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.
Connect Chart to MATLAB App
The chart App Interface is already configured to communicate with the MATLAB app sfMediaPlayerApp. To create a bidirectional connection between your MATLAB app and a Stateflow chart that uses C as the action language, follow these steps.
In the MATLAB app:
Create a custom property to interface with the chart during simulation. The app uses this property to access chart inputs, chart outputs, and local data. For more information, see Share Data Within App Designer Apps.
Modify the
startupFcncallback for the app by adding a new input argument. Store the value of this input argument as the property that you created in the previous step. For more information, see Callbacks in App Designer.
In the Stateflow chart:
Create a local data object to interface with the app. The chart uses this local data object as an argument when it calls helper functions in the app.
Set the type of the local data object you created in the previous step to
ml. For more information, see Specify Scope and Type of Stateflow Data.Run the app using the
mlnamespace operator to indicate that the app is extrinsic MATLAB code. Pass the keywordthisas an argument to give the app access to the chart during simulation. Store the value returned by the function call to the app as the local data object that you created to interface with the app. For more information, see Access MATLAB Functions and Workspace Data in C Charts.
In this example, the Media Player App uses a property called chart to interface with the chart App Interface. The app callbacks use this property to write to the chart outputs:
When you turn the Component Selection knob to select a component of the media player, the callback
ComponentKnobValueChangedsets the value ofCompRequest.When you click the Pause, Play, Reverse, or Fast-Forward buttons, the corresponding callbacks set the value of
StreamRequest.When you click Connect, the callback
ConnectButtonPushedsets the value ofStreamName.When you close the app, the callback
UIFigureCloseRequestsets the value ofStoptotrue.
Conversely, in the chart, the entry actions in the InterfaceWithApp state run the app sfMediaPlayerApp and store the returned value as the local data object app. The chart uses this local data object when it calls the helper functions updateStatus and updateButtons. In the app, these helper functions update the media player status message and enable, disable, or change the color of the playback buttons based on the value of the chart inputs Component, StreamMode, and StreamStatus.

Manage Media Player Modes by Using Enumerated Data
The Mode Manager chart determines the behavior of the media player based on the inputs received from the App Interface chart. The inputs and outputs for this chart are enumerated data that describe the operating modes of the media player and playback modes of the stream player component.
The chart input
CompRequestand the chart outputComponentuse the enumerated data typeMediaComponentMode, which has values that correspond to the components of the media player:
classdef MediaComponentMode < Simulink.IntEnumType enumeration OFF(0), RADIO(1), STREAM(2) end end
The chart input
StreamRequestand the chart outputStreamModeuse the enumerated data typeStreamingMode, which has values that correspond to the operating modes of the stream player:
classdef StreamingMode < Simulink.IntEnumType enumeration PAUSE(0), PLAY(1), REW(2), FF(3) end end
By using enumerated data to group related values into separate data types, you can enhance the readability of your chart and reduce the number of constant data objects in your model. For more information, see Reference Values by Name by Using Enumerated Data.

The chart contains two top-level states, On and Off, that define the activity of the media player. At the start of simulation, the state Off is active. The transitions between On and Off depend on whether or not the chart input CompRequest equals the enumerated value OFF.
In the On state, the substates Stream and Radio represent the operating modes of the media player. The inner transition in this state uses the operator hasChanged to continually scan for changes in the value of CompRequest and activate one of these substates:
The substate
Streambecomes active when you select the stream player and the value ofCompRequestchanges toSTREAM.The substate
Radiobecomes active when you select the FM radio and the value ofCompRequestchanges toRADIO.
In the Stream state, the substates Play and Pause determine the activity of the stream player. The transitions between these substates depend on the value of the chart input StreamRequest:
The substate
Playbecomes active when you play a stream andStreamRequestchanges to the enumerated valuePLAY.The substate
Pausebecomes active when you pause the stream player andStreamRequestchanges to the enumerated valuePAUSE.
Finally, in the Play state, the substates Normal, Reverse, and FastForward represent the play modes for the stream player. The inner transitions in this state monitor the value of StreamRequest:
The substate
Normalis active during normal play mode, when the value ofStreamRequestequalsPLAY.The substate
Reversebecomes active when you click the Reverse button and the value ofStreamRequestchanges toREW.The substate
FastForwardbecomes active when you click the Fast-Forward button and the value ofStreamRequestchanges toFF.
The Mode Manager chart communicates with the App Interface chart by setting the value of the output Component to OFF when the state Off is active, or to STREAM or RADIO depending on which substate of On is active. Similarly, the chart communicates with the Stream Player chart by setting the value of the output StreamMode to PAUSE when the state Pause is active, or to PLAY, REW, or FF depending on which substate of Play is active.
Compose Stream Player Status by Using String Data
The Stream Player chart simulates the internal behavior of the stream player and provides natural language output to the app by using string data. The chart uses the string operators strcat, strlen, and substr to concatenate strings and create the effect of scrolling text in the output string StreamStatus. For more information, see Manage Textual Information by Using Strings.

The chart output StreamStatus contains a status message that depends on the chart inputs StreamMode and StreamName:
When the value of
StreamModeequalsPLAY, the entry action in the statePLAYsets the status message to the string"Playing:"followed by the value ofStreamName.When the value of
StreamModeequalsREW, the entry and during actions of the stateREWset the status message to the string"Reverse <<"followed by a substring ofStreamName. The substring continually changes length to produce a backward scrolling effect.When the value of
StreamModeequalsFF, the entry and during actions of the stateFFset the status message to the string"Forward >>"followed by a substring ofStreamName. The substring continually changes length to produce a forward scrolling effect.When the value of
StreamModeequalsPAUSE, the entry action in the statePAUSEsets the status message to the string"Paused:"followed by the value ofStreamName.When the value of
StreamNamechanges, the entry action in the stateConnectingsets the status message to the string"Connecting to"followed by the value ofStreamName.
The Stream Player chart outputs this status message to the App Interface chart. When the stream player is on, the App Interface chart calls the helper function updateStatus, which displays the status message in the Media Player Status box at the top of the Media Player App.
See Also
this | hasChanged | strcat | strlen | substr