21

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?

2 Answers 2

23

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.

Sign up to request clarification or add additional context in comments.

Comments

10

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'));
...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.