This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

DataAvailable

Notify when acquired data is available to process

Syntax

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

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 (MATLAB).

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.

Introduced in R2010b