0

My Matlab code looks something like this:

for t = 1:T
   arg = (do some calculation)
   func(arg)
end

I know that there is something wrong with the calculation in the loop that causes an error inside the body of func. But if I set the debugger to stop on an error, it will stop inside the body of func. What I really need is to step outside of func and into the for loop to see what calculation went wrong. T is a huge number so manually stepping through is not an option. I also cannot pass t to func because the entire piece of code is read-only. Is there a way for the debugger to step out from func when an error occurs?

1

2 Answers 2

2

Once your function stops in the debugger, you can switch up one (or several) level(s) of the stack in order to check what went wrong in the calling function.

The easiest way to do this is probably via the GUI, where you can use the drop-down menu to switch between workspaces, though alternatively, you can use DBUP on the command line.

R2012a and earlier: enter image description here

R2012b and later: enter image description here

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

1 Comment

Thanks a lot. It's so obvious but somehow I didn't notice it.
1

Try using try:

for t = 1:T
   arg = (do some calculation)
   try
       func(arg)
   catch
       z = 1;
   end
end

Then just set your debug point on the line z = 1;. When the error in func trips, the program will jump to the z = 1; line, where your debug point will halt things and allow you to examine the workspace. Done!

3 Comments

The problem is I do not have write permission to modify the code. I have to rely on the debugger.
@ezbentley copy the file where you can write, add path to matlabs path, and modify the file as Colin suggested. Your version will be executed, since the path will shadow the other existing function.
@ezbentley Oops, sorry, I clearly didn't read the question carefully enough! angainor's suggestion should get you round that problem, but otherwise, Jonas's suggestion is very neat, and is not a feature I was aware you could manipulate from the GUI. +1!

Your Answer

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