Main Content


(Not recommended) Create event listener

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



lh = addlistener(s,eventName,@callback) creates a listener for the specified event, eventName, to execute the callback function, callback at the time of the event. lh is the variable in which the listener handle is stored. Create a callback function that executes when the listener detects the specified event. The callback can be any MATLAB® function.


Delete the listener once the operation is complete.



lh = addlistener(s,eventName,@(src,event) expr) creates a listener for the specified event, eventName, and fires an anonymous callback function. The anonymous function uses 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 them in a file. For more information, see Anonymous Functions.


collapse all

Creating a session and add an analog input channel.

s = daq.createSession('ni');

Add a listener for the DataAvailable event.

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

Create the plotData callback function and save it as plotData.m.

function plotData(src,event)

Acquire data in the background.


Wait for the operation to complete, and delete the listener.


Create a session and set the IsContinuous property to true.

s = daq.createSession('ni');
s.IsContinuous = true;

Add two analog output channels and create output data for the two channels.

outputData0 = linspace(-1,1,1000)';
outputData1 = linspace(-2,2,1000)';

Queue the output data.

queueOutputData(s,[outputData0 outputData1]);

Add a listener to call an anonymous function.

lh = addlistener(s,'DataRequired', @(src,event)...
      src.queueOutputData([outputData0 outputData1]));

Generate signals in the background.


Perform other MATLAB operations, and then stop the session. If the interim tasks do not allow enough time for the signal generation, use a pause before stopping.


Delete the listener.


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.

Name of the event to listen for, specified as a character vector or string. Supported events include:

Data Types: char | string

The callback function to execute, specified as a function handle. The function executes when the specified event occurs.

Session input argument to the anonymous function, specified as a variable name. addlistener sends the data acquisition session object handle into the anonymous function as this variable.

Event input argument to the anonymous function, specified as a variable name. addlistener sends the triggering event object handle into the anonymous function as this variable.

Body of anonymous function, specified as a line of executable text. The expression can include the input argument variables names src and event.

Output Arguments

collapse all

The event listener returned as an event object handle. Delete the listener once the operation completes.

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.