This is machine translation

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

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.


Create event listener


lh = addlistener(s,eventName,@callback)
lh = addlistener(s,eventName,@(src,event) expr)



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


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.

delete (lh)

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.


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.

Introduced in R2010b

Was this topic helpful?