Documentation

DataAvailable

Notify when acquired data is available to process

Syntax

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

Description

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:

Data

An mxn 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 mx1 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

Create DataAvailable Function

This example shows how to create an event that plots data when triggered using a callback function.

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 plot the data:

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)

Create Anonymous DataAvaialble Function

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)

Related Examples

Was this topic helpful?