ErrorOccurred Event
(Not recommended) Notify when device-related errors occur
This session
object function is not recommended. Use DataAcquisition
object functions instead. See Version History.
Syntax
Description
lh = addlistener(
creates a listener for the session
,'ErrorOccurred',callbackfct
);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.
lh = addlistener(
creates a listener for the session
,'ErrorOccurred',@(src,event)
expr
);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.
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
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)