Main Content

Analog Triggered Data Acquisition Using Stateflow Charts

This example shows how to create an analog-triggered data acquisition app by using Stateflow®, Data Acquisition Toolbox™, and App Designer.

Data Acquisition Toolbox provides functionality for acquiring measurement data from a DAQ device or audio soundcard. For certain applications, an analog-triggered acquisition that starts capturing or logging data based on a condition in the analog signal being measured is recommended. Software-analog triggered acquisition enables you to capture only a segment of interest out of a continuous stream of measurement data. For example, you can capture an audio recording when the signal level passes a certain threshold.

This example app, created by using App Designer and Stateflow, shows how to implement these operations:

  • Control the app state logic by using a Stateflow chart.

  • Discover available DAQ devices and select which device to use.

  • Configure device acquisition parameters.

  • Display a live plot in the app UI during acquisition.

  • Perform a triggered data capture based on a programmable trigger condition.

  • Save captured data to a MATLAB® base workspace variable.

By default, the app opens in design mode in App Designer. To run the app click the Run button or execute the app from the command line:



This example app requires:

  • MATLAB R2020a or later.

  • Data Acquisition Toolbox (supported on Windows® only).

  • Stateflow (for creating and editing charts only).

  • A supported DAQ device or sound card. For example, any National Instruments or Measurement Computing device that supports analog input Voltage or IEPE measurements and background acquisition.

  • Corresponding hardware support package and device drivers.

App States and the Stateflow Chart

When creating an app that has complex logic, consider the various states that correspond to the operating modes of the app. You can use a Stateflow chart to visualize and organize these app states. Use transitions between states to implement the control logic of your app. For example, the file AnalogTriggerAppLogic.sfx defines the Stateflow chart that controls the logic for this app. The chart can transition between states based on an action in the app UI or on a data-driven condition. For example, if you click the Start button, the chart transitions from the Configuration state to the Acquisition state. If the value of the signal crosses the specified trigger level, the chart transitions from the LookingForTrigger state to the CapturingData state.

Integrating the App with the Stateflow Chart

To establish a bidirectional connection between the MATLAB app and the Stateflow chart, in the startupFcn function of your app, create a chart object and store its handle in an app property.

app.Chart = AnalogTriggerAppLogic(app=app);

The app uses this handle to trigger state transitions in the chart. For example, when you click Start, the StartButtonPushed app callback function calls the acquisitionStart input event for the chart. This event triggers the transition from the Configuration state to the Acquisition state.

To evaluate transition conditions that are not events in the chart, the app calls the step function for the chart object. For example, while acquiring data from the device, the dataAvailable_Callback app function periodically calls the step function. When the trigger condition is detected, the chart transitions from the LookingForTrigger State to the CapturingData state.

In the Stateflow chart, store the app object handle as chart local data. To share public properties and call public functions of the app, the Stateflow chart can use this handle in state actions, transition conditions, or transition actions.

See Also

Related Topics