1

I wonder what the difference is between entering a few lines in the command window, or letting a script execute them.

In the question Escape from nested try - catch statement I have an example function. I have put the selected code in a script and called it, however then it does not work properly. On the other hand, when I select the lines and hit f9, it works as expected.

The lines are:

dbclear all
dbquit
dbstop if caught error

I call the example function as such:

dbstop if caught error
mytestmain

And the example function is:

function mytestmain 
try
    mytestsub
catch
end

% Definition of subfunction, may or may not be in the same .m file
function mytestsub
try
    a=b; %Intentionally generate an error as b is not defined
catch
end
1
  • When you say "does not work properly" and "works as expected", what specifically is happening, and what behavior do you expect? Commented Jun 8, 2013 at 4:32

2 Answers 2

3

I think it's related to MATLAB's just-in-time (JIT) compiler, which compiles functions before it runs them.

It seems that it compiles functions differently if dbstop is set or not (see here for reference). As it currently stands, MATLAB can not recompile a function while it is run (just try saving a changed function during a dbstop, and you will get a message informing you). As you can add and remove breakpoints during a dbstop I think you can also do so programmatically, but it should be impossible to "turn on" debugging if it wasn't turned on at "compile time"

So in your cases:

  • Using F9 it's just pasted and parsed as if you input it manually. So first dbstop is set, then mytestmain gets compiled and executed.
  • Running as a script will first compile the script and mytestmain and then execute it - so dbstop would be set after compilation and therefore not in effect.
Sign up to request clarification or add additional context in comments.

4 Comments

I suppose it could be a compilation issue, but I do notice that it also occurs with feature accel off.
I also found info about this command. But it seems to be missing in my matlab version (2012b) - how did you turn it off?
I have found it here, not sure if it actually still works though: undocumentedmatlab.com/blog/undocumented-feature-function
oh interesting. the IsDebug and JIT options look interesting as well. Also did you clear your memory in between - my be Matlab cached the old compiled version.
0

Depending on what you mean by "doesn't work", it could just be because the debugger is a special context and certain debugger commands - dbup, dbdown, and dbquit - only work when you're at a debugger "K>>" prompt. Once you call a script, you're no longer at the debugger prompt but in normal code execution - inside a nested M-code call stack - and they just don't work there. When you F9, it does the lines individually, so each one is done from the prompt.

As a workaround, if you really want to execute a sequence of debugger commands like this, you could write a little Java Swing widget to enter the text in to the command window just as though you were typing it in.

1 Comment

I can imagine that the commands (second dbquit and further) go to the wrong prompt. However, in that case I would expect the notification that dbquit is only allowed in debug mode. Now simply nothing happens.

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.