ErrorOccurred Event

Notify when device-related errors occur

Description

example

lh = addlistener(session,'ErrorOccurred',callbackfct); creates a listener for the ErrorOccurred event. When an error occurs, the callback is executed. The callback can be any MATLAB® function with the (src,event) signature.

Note

In background mode, errors and exceptions are not displayed by default. Use the ErrorOccurred event listener to display the errors.

example

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 requiring that your function be saved in a separate file. For more information, see Anonymous Functions (MATLAB).

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 can use the MException.getReport method to return a formatted message that uses the same format as errors thrown by internal MATLAB code.

Examples

Add a Listener to Display an Error Report

Create a session, and add an analog input channel.

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

Get a formatted report of the error.

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

Acquire data, wait, and delete the listener.

startBackground(s);
wait(s)
delete(lh)

Input Arguments

collapse all

Data acquisition session, specified as a session object. Create the session object using daq.createSession. Use the data acquisition session for acquisition and generation operations. Create one session per vendor and use that vendor session to perform all data acquisition operations.

Callback function, specified as a function handle.

Anonymous callback function, specified as a MATLAB operation. The expression executes when the trigger occurs.

Introduced in R2010b