MATLAB: How to get the ERROR function to only display the error message when used in a function or user defined object method without the line of code being displayed

MATLAB

When the ERROR function is used in a file, the error message is preceded by information about where the error occurred, and the stack field of the structure returned by LASTERROR. The stack field contains a structure array in the same format as the output of dbstack('-completenames'). This stack points to the line, function, and file in which the error occurred.
I want to use ERROR to display only the error message, leaving out the information about the stack.

Best Answer

The ability to use the ERROR function to display only the error message is not available in MATLAB 7.6 (R2008a). You can make use of try-catch blocks as a workaround. For example:
clear all
try
% Enter your code here
A
catch ME
myerrstr=strcat('Error:',ME.message);
fprintf(2, '%s \n', myerrstr)
end
The example executes the statements in the TRY block. If it encounters any errors, it skips the rest of the statements in the TRY block and continues executing statements in the CATCH block. The CATCH command marks the start of a catch block and provides access to a data structure that contains information about what caused the exception. This is shown as the variable ME in the this example. ME is an object of the MATLAB MException class. When an exception occurs, MATLAB constructs an instance of this class and returns it in the CATCH statement that handles that error and prints the desired error message in red color.