I'm programming up a custom class and want to throw an undefined operator error (like would occur if you executed {[]}*{[]}). I know I need to make an error structure with the message, the stack, and the identifier. It's the last that I'm having trouble with, I don't understand how MException works. I tried out the following code
myError.stack = dbstack;
myError.identifier = MException('foo:noBar','You did not foo bar.');
myError.message = ['You did not foo bar.'];
error(myError)
but get the error
Error using error
Field "identifier" of input structure must contain a valid MATLAB message identifier.
But I don't know what valid identifiers are because the help section just seems to go in circles.
Is there a list of valid identifiers that I'm missing? Or some additional resource?
1 Answer 1
The error command will create the MException object for you, and include call stack information. You should not create these yourself.
Simply call the error command with your new ID and message:
error('foo:noBar','You did not foo bar.');
If you really want to use a struct for this, put those two pieces of information into the identifier and message fields:
myError.identifier = 'foo:noBar';
myError.message = 'You did not foo bar.';
error(myError)
But this is just a more verbose version of the former. I guess this could be useful if you keep a struct array with errors, so you just need to do error(myErrors(3)), that way your messages and IDs will be consistent.