Notify when acquired data is available to process


lh = addlistener(session,'DataAvailable',callback);
lh = addlistener(session,'DataAvailable', @(src, event), expr)


lh = addlistener(session,'DataAvailable',callback); creates a listener for the DataAvailable event. When data is available to process, the callback is executed. The callback can be any MATLAB® function with the (src, event) signature.

lh = addlistener(session,'DataAvailable', @(src, event), expr) creates a listener for the DataAvailable event and fires an anonymous callback function. The anonymous function requires the specified input arguments and executes the operation specified in the expression expr. Anonymous functions provide a quick means of creating simple functions without storing your function to a file. For more information see Anonymous Functions.

The callback has two required parameters: src and event. src is the session object for the listener and event is a daq.DataAvailableInfo object containing the data associated and timing information. Properties of daq.DataAvailableInfo are:


An mxn matrix of doubles where m is the number of scans acquired, and n is the number of input channels in the session.


The timestamps relative to TriggerTime in an mx1 array where m is the number of scans acquired.


A MATLAB serial date time stamp representing the absolute time the acquisition trigger occurs.


Create a session, add an analog input channel, and change the duration of the acquisition:

s = daq.createSession('ni');
addAnalogInputChannel(s,'cDAQ1Mod1', 'ai0', 'Voltage');

To add a listener for the DataAvailable event to plot the data, type:

lh = addlistener(s,'DataAvailable', @plotData);

Create a function that plots the data when the event occurs:

 function plotData(src,event)
     plot(event.TimeStamps, event.Data)

Start the acquisition and wait:


Delete the listener:

Was this topic helpful?