Notify when device-related errors occur
lh=session.addlistener('ErrorOccurred' @(src,event), expr);
lh=session.addlistener('ErrorOccurred', callback); creates a listener for the ErrorOccurred event. When an error occurs, the call back is executed. The callback can be any MATLAB® function with the (src, event) signature.
lh=session.addlistener('ErrorOccurred' @(src,event), expr); creates a listener for the ErrorOccurred event and fires an anonymous function. The anonymous function requires 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 your function to a file. For more information, see Anonymous Functions.
The callback has two required parameters: src and event. src is the session object for the listener and event is a daq.ErrorOccurredInfo object. The daq.ErrorOccurredInfo object contains the Error property, which is the MException associated with the error. You could use the method to return a formatted message string that uses the same format as errors thrown by internal MATLAB code.
Create a session, and add an analog input channel:
s = daq.createSession('ni'); s.addAnalogInputChannel('cDAQ1Mod1', 'ai0', 'Voltage');
To get a formatted report of the error, type:
lh = s.addlistener('ErrorOccurred' @(src,event), disp(event.Error.getReport()));
Acquire data, wait and delete the listener:
s.startBackground(); s.wait() delete(lh)