Main Content

DataAvailable

(Not recommended) Notify when acquired data is available to process

This session object function is not recommended. Use DataAcquisition object functions instead. See Compatibility Considerations.

Description

example

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

Tip

The frequency with which the DataAvailable event is fired, is controlled by NotifyWhenDataAvailableExceeds

example

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 in a separate 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:

Data

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

TimeStamps

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

TriggerTime

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

Examples

collapse all

This example shows how to create an event that triggers a callback function to plot data.

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

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

Add a listener for the DataAvailable event to trigger the plotting callback.

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)
end

Start the acquisition and wait.

startBackground(s);
wait(s)

Delete the listener.

delete(lh)

This example shows how to create an event using an anonymous function call to plot data when an event 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');
s.DurationInSeconds = 5;

Add a listen with an anonymous function call.

lh = s.addlistener('DataAvailable', ...
          @(src,event) plot(event.TimeStamps, event.Data));

Acquire data.

s.startBackground();

Delete the listener.

delete(lh)

Input Arguments

collapse all

Data acquisition session, specified as a session object. Create the session object using daq.createSession. Use the data acquisition session for acquisition and generation operations. Create one session per vendor and use that vendor session to perform all data acquisition operations.

Callback function, specified as a function handle.

Anonymous callback function, specified as a MATLAB operation. The expression executes when the trigger occurs.

Version History

Introduced in R2010b

collapse all

R2020a: session object interface is not recommended

Use of this function with a session object is not recommended. To access a data acquisition device, use a DataAcquisition object with its functions and properties instead.

For more information about using the recommended functionality, see Transition Your Code from Session to DataAcquisition Interface.