Data Acquisition Toolbox    

Examples: Using Callback Properties and Functions

This section provides examples that show you how to create callback functions and configure callback properties.

Displaying Event Information with a Callback Function

This example illustrates how callback functions allow you to easily display event information. The example uses daqcallback to display information for trigger, run-time error, and stop events. The default SampleRate and SamplesPerTrigger values are used, which results in a 1-second acquisition for each trigger executed.

You can run this example by typing daqdoc5_6 at the MATLAB command line.

  1. Create a device object -- Create the analog input object AI for a sound card. The installed adaptors and hardware IDs are found with daqhwinfo.
  2. Add channels -- Add one hardware channel to AI.
  3. Configure property values -- Repeat the trigger three times, find the time for the acquisition to complete, and define daqcallback as the M-file to execute when a trigger, run-time error, or stop event occurs.
  4. Acquire data -- Start AI and wait for it to stop running. The waittilstop function blocks the MATLAB command line, and waits for AI to stop running.
  5. Clean up -- When you no longer need AI, you should remove it from memory and from the MATLAB workspace.

Passing Additional Parameters to a Callback Function

This example illustrates how additional arguments are passed to the callback function. Timer events are generated every 0.5 second to display data using the local callback function daqdoc5_7plot (not shown below).

You can run this example by typing daqdoc5_7 at the MATLAB command line.

  1. Create a device object -- Create the analog input object AI for a sound card. The installed adaptors and hardware IDs are found with daqhwinfo.
  2. Add channels -- Add one hardware channel to AI.
  3. Configure property values -- Define a 10-second acquisition and execute the M-file daqdoc5_7plot every 0.5 seconds. Note that the variables bsize, P, and T are passed to the callback function.
  4. Acquire data -- Start AI. The drawnow command in daqdoc5_7plot forces MATLAB to update the display. The waittilstop function blocks the MATLAB command line, and waits for AI to stop running.
  5. Clean up -- When you no longer need AI, you should remove it from memory and from the MATLAB workspace.

  Creating and Executing Callback Functions Linearly Scaling the Data: Engineering Units