| Contents | Index |
Create event listener
lh = addlistener('eventName',@callback)
lh = addlistener('eventName',
@(src, event) expr)
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.
You must delete the listener once the operation is complete.
delete (lh)
'eventName' |
Name of the event to listen for. Available events include:
|
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. |
lh |
Handle to the event listener returned by addlistener. Delete the listener once the operation completes. |
Add a listener to an acquisition session by first:
Creating a session
Adding an analog input channel
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)
endAcquire 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:
Creating a session.
Setting IsContinuous to true.
Adding two analog output channels.
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)
daq.createSession | daq.Session.addAnalogInputChannel | daq.Session.addAnalogOutputChannel | daq.Session.startBackground | DataAvailable | DataRequired | ErrorOccurred

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 |