Model a Fitness App by Using Standalone Charts
This example shows how to create an application composed of multiple standalone Stateflow® charts and a MATLAB® app. The standalone charts model the control and plant systems for the application and interface with the MATLAB app. For more information on connecting a standalone chart to a MATLAB app, see Design Human-Machine Interface Logic by Using Stateflow Charts. For a version of this example that uses Stateflow charts in a Simulink® model, see Model a Fitness Tracker.
In this example, a MATLAB app models a fitness tracker. When you run the app, you can adjust the settings for the tracker and select an activity (Sleep, Rest, Walk, or Exercise). When you choose Exercise, you can also set the intensity of your workout.
The standalone chart
sf_fitness_interface provides a bidirectional connection between the MATLAB app and the other standalone charts in the example,
sf_fitness_tracker. These charts model a human simulator and provide the core logic for the fitness tracker, respectively. When you interact with the widgets in the app, the
sf_fitness_interface chart communicates your selections to the other charts in the example. Conversely, the chart uses the output of the fitness tracker to update the numeric and text fields in the app.
This schematic diagram illustrates the transfer of information between the app and the charts in the example.
To start the example, run the
sf_fitness_sfx_app app. The app creates a chart object for
sf_fitness_interface. This chart, in turn, creates chart objects for the other two charts in the example. The chart also creates a MATLAB
timer object that executes all three charts at a frequency proportional to the heart rate in the human simulator chart. While the example is running, one second of simulation represents one minute of exercise time. To stop the example, close the app.
Connect Chart to MATLAB App
sf_fitness_interface is configured to communicate with the MATLAB app
The chart uses the local data object
appto interface with the MATLAB app. The chart uses this local data object when it calls the helper functions
updateHeartRate. In the app, these helper functions change the contents of the activity status, clock, and step counter fields, and create the animation effects in the heartbeat and footstep displays. For example, when there is a new notification from the fitness tracker, the substate
MainDisplaycalls the helper function
updateText. This function replaces the contents of the clock display with a customized notification. After a short delay, the substate calls the helper function
updateClockto restore the clock display.
The app uses a property called
chartto interface with the chart. The app uses this property to read the chart local data. For example, the helper functions
updateStepsread the chart local data
total_steps, respectively. Additionally, when you close the app, the
UIFigureCloseRequestcallback uses the
chartproperty to stop the execution of the charts in the example by deleting their chart objects.
For more information on how to create a bidirectional connection between your MATLAB app and a standalone Stateflow chart, see Design Human-Machine Interface Logic by Using Stateflow Charts.
To establish communications with the human simulator and fitness tracker charts, the
sf_fitness_interface chart saves their chart objects as the local data
tracker. The chart-level MATLAB functions
updateTracker use these objects to write to and read from the local data in the charts. For example, when you select a new activity or change the intensity of your workout in the Human Simulator pane of the app,
updateHuman sets the value of the local data
intensity in the human simulator chart. Similarly, when you change the value of one of the fields in the Settings pane of the app,
updateTracker updates the value of the corresponding local data in the fitness tracker chart.
Simulate Vital Signs Based on Activity
The human simulator chart
sf_fitness_human models the vital signs of a human engaged in the activity you select in the app. The chart stores these vital signs (representing your heart rate, speed, and the number of steps that you have taken) as local variables that the fitness tracker can access. When you select a new activity or adjust the intensity of your workout, the chart calls the function
transition to ensure that these vital signs change gradually over time. To detect changes in activity or exercise intensity, the chart calls the
hasChanged operator. For more information, see Detect Changes in Data and Expression Values.
Determine Fitness Tracker Output
sf_fitness_tracker models the core logic of the fitness tracker. The chart consists of four subcharts that correspond to the possible activities. The chart registers your activity status based on the heart rate and speed produced by the human simulator chart and transitions between these subcharts. To filter out signal noise, the chart uses the
count operator to implement simple debouncing logic. For instance, when you are at rest, you can make some quick and sudden movements that do not correspond to exercise. The chart determines that you are walking or exercising only if your motion lasts longer than two evaluations of the chart object.
The chart uses other temporal logic operators to track the amount of time you spend in each activity and determine when to send notifications to the app:
The exit actions in each subchart call the
elapsedoperator to determine how long the subchart was active.
The chart uses the
afteroperator to determine when you sleep or walk for longer than five minutes, rest or exercise for longer than the threshold you specify in the app, or exercise at a high intensity (taking more than 4 steps a second) for longer than 15 minutes. In each of these cases, the chart updates the value of the local data
sf_fitness_interfacechart reads this value and causes a notification to appear in the main display of the app. Depending on the type of notification, the notification button changes color.