Skip to Main Content Skip to Search
Product Documentation

addlistener - Class: daq.Session
Package: daq

Create event listener

Syntax

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

Description

lh = addlistener('eventName',@callback) creates a listener for the specified event, eventName, and fires the callback function, callback. 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.

lh = addlistener('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 to a file. For more information, see Anonymous Functions.

Tips

Input Arguments

'eventName'

Name of the event to listen for. Available events include:

  • 'DataAvailable'

  • 'DataRequired'

  • 'ErrorOccurred'

callback

Name of the function to execute when the specified event occurs.

src

The session object, where the event occurred.

event

Specified event object. For more information, see Session Events.

expr

Expression that represents the body of the function.

Output Arguments

lh

Handle to the event listener returned by addlistener. Delete the listener once the operation completes.

Examples

Add a listener to an acquisition session by first:

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

Add a listener for the DataAvailable event:

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

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

function plotData(src,event)
         plot(event.TimeStamps, event.Data)
end

Acquire data in the background:

s.startBackground();

Wait for the operation to complete and delete the listener:

delete (lh)
 

Add a listener using an anonymous function to a signal generation session by first:

s = daq.createSession('ni');
s.IsContinuous = true;
s.addAnalogOutputChannel('cDAQ1Mod2', 0:1, 'Voltage');

Create output data for the two channels:

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

Queue the output data:

s.queueOutputData([outputData0 outputData1]);

Add an anonymous listener and generate the signal in the background:

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

Generate signals in the background:

s.startBackground();

Perform other MATLAB operations, and then stop the session.

s.stop ()

Delete the listener:

delete (lh)

See Also

daq.createSession | daq.Session.addAnalogInputChannel | daq.Session.addAnalogOutputChannel | daq.Session.startBackground | DataAvailable | DataRequired | ErrorOccurred

How To

  


Recommended Products

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.

 © 1984-2012- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS