This example shows how to acquire data in the background using events and listeners.
A background acquisition depends on events and listeners to
allow your code to access data as the hardware acquires it and to
react to any errors as they occur. For more information, see Events
and Listeners — Concepts in the MATLAB Object-Oriented Programming
documentation. Use events to acquire data in the background. In this
example, you acquire data from an NI 9205 device with ID cDAQ1Mod1
using a listener and a
Listeners execute a callback function when notified that the
event has occurred. Use
create a listener object that executes your callback function.
Create an NI session object and an analog input 'Voltage' channel on cDAQ1Mod1:
s = daq.createSession('ni'); addAnalogInputChannel(s,'cDAQ1Mod1', 'ai0', 'Voltage');
Add the listener for the
and assign it to the variable
lh = addlistener(s,'DataAvailable', @plotData);
For more information on events, see Events and Listeners — Concepts in the MATLAB Object-Oriented Programming documentation.
Create a simple callback function to plot the acquired
data and save it as
plotData.m in your working
function plotData(src,event) plot(event.TimeStamps, event.Data) end
src is the session object for the
event is a
containing the data and associated timing information.
Acquire the data and see the plot update while MATLAB® is running:
When the operation is complete, delete the listener: