Documentation

This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

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

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

Examples

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)
Was this topic helpful?