Contents

ErrorOccurred Event

Notify when device-related errors occur

Syntax

lh = addlistener(session,'ErrorOccurred', callback);
lh = addlistener(session,'ErrorOccurred',@(src,event)expr);

Description

lh = addlistener(session,'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 = addlistener(session,'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 MException.getReport method to return a formatted message string that uses the same format as errors thrown by internal MATLAB code.

Examples

Create a session, and add an analog input channel:

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

To get a formatted report of the error, type:

lh = addlistener(s,'ErrorOccurred' @(src,event), disp(event.Error.getReport()));

Acquire data, wait and delete the listener:

startBackground(s);
wait(s)
delete(lh)
Was this topic helpful?