MATLAB: Does LASTERROR sometimes return a message string even if no operations have failed

abeData Acquisition Toolboxhandlelasterrormustparameterset

I am able to execute the following code without errors in MATLAB.
lasterror('reset');
ai = analoginput('winsound');
chan = addchannel(ai,1);
set(ai,'TriggerChannel',chan);
error_stat = lasterror;
At the end of the code execution, the lasterror message string is not empty even though the code has executed successfully.

Best Answer

This is the expected behavior. Some functions try an operation, find that it fails, then recover and use a different operation to accomplish their goals. The function then returns successfully, but the error message (which has already been worked around) is listed by LASTERROR.
Because of this case, the string in the "message" field of the structure returned by LASTERROR is not guaranteed to be empty even if a MATLAB function call is successful.
Instead of relying on the content of the message field of the structure returned by LASTERROR to detect errors, it is recommended that the user use the TRY-CATCH statements to handle runtime errors.
You can find more information about the TRY and CATCH commands by entering this command in MATLAB::
web([docroot,'/techdoc/ref/try.html'])