Showing complete error stack while error handling

I have a matlab script that calls various other function. I am handling possible error in the following way

            try
                 matStart(MatObj);
             catch err
                 msgbox('Error in Processing Figures!','Error!','error','modal');
                 fprintf(2,err.message);
                 sprintf('\n');
                 display(err.message);
            end

as you can probably guess, this prints the error that caused the exception.But this only prints the very first function that caused the error. I want the whole error stack to be shown down to the last nested function that caused the error to occur. Can tis be done?

Answers


Yes, the function you're looking for is "getReport". You'll want the 'extended' report.

Using getReport, your code would look like this

        try
             matStart(MatObj);
         catch err
             msgbox('Error in Processing Figures!','Error!','error','modal');
             disp(getReport(err,'extended'));
        end

This will display the same information as an uncaught exception in matlab that prints the full stack trace, though of course the text won't be red.


Following on from @thewopr's answer, you can have the text printed in red if you wish by printing the error stack to the 'standard error' output stream, like so:

...
fprintf(2, '%s\n', getReport(err, 'extended'));
...

Need Your Help

Remove a directory from all previous commits

git version-control

Git Repo, there is a folder ABC with three files, all having spaces in the name file - 1.ext, file - 2.ext, and file - 3.ext. I want to remove the folder and the files. I am only aware of how to

Eclipse Android - Logcat Clearing too Fast

android eclipse logcat

I have been using Eclipse for Android (most up to date version), for a while with no problems with the Logcat. For an unknown reason, Logcat is no longer retaining the debug messages. Logcat is get...