Matlab debug: skip next line without execution

2019-07-24 04:40发布

问题:

Questions: (full description of problem is below)

  • Does anyone have a suggestion on how to trick Matlab into skipping a line or several lines of code? (mex / java / rewriting some internal Matlab features?)
  • Does anyone know where db* code files may be located (if exist)?

There are several functions in Matlab that allow flow-control while debugging / running a program: dbstop, dbcont, dbstep, etc... I was trying to find a db* function that would skip the next line in a Matlab script, dbskip perhaps, but sadly there is no such builtin functionality and it doesn't seem like this feature going to be ever implemented (10 years and counting).

EDIT example of use:

The following script, myScript, may call a function myFunc which decides on whether to skip the next line in the caller script based on the value of its input, like so:

% myScript.m 
x = 1;
myFunc(x);
x = 2;
x = 3;

% myFunc.m
function myFunc(u)
   if u == 1
       % skip next line in caller -- how to?
       % ... do more stuff ...
   else
       % ... do other stuff ...
       disp('Business as usual.')
   end
   % ... do even more stuff ...
end

EDIT

One way of achieving this, as @Michael Smith suggested, is to write a program that would read myScript.m and execute it line by line skipping any line that is specified at runtime. I believe that this solution is a good start; however, in more complex cases, there would be function calls within function calls. So, making a truly general solution using this approach, would require writing a full blown Matlab code interpreter having all the capabilities that Matlab has plus some of my own requirements. This is something I would like to avoid if I can use some of Matlab's inherent reflective capabilities or by hacking into the debugging system.

In the meanwhile, I hit another wall trying to find how and where dbstep is implemented. When debugging any of thedb* functions the result is the following error:

Error using <db*> 
Debug commands only allowed when stopped in debug mode.

The only dbstep.m file I could find, contains documentation without any code so I assume that it is a compiled built-in.

回答1:

Alright, this might get a little messy. I can think of two possible ways to do this. One of them involves editing your script to have every line have an if statement looking at a local variable that you can modify in the workspace. I'm going to just call this one out. So lets look at the next idea.

matlab has a command eval(EXPRESSION) that takes in a string and executes. Here's the thought. Lets create a script/function for debugging with something like the following:

FID = fopen('<filename here>');
no_skip = true;
line_no = 1;

executing_script = 1;
while(executing_script == 1)
    line_no              %Print the line number
    line = fgetl(FID)    %removed the ; so it will write the line
    if(no_skip)
       eval(line)
    end
    line_no = line_no + 1;
end

Having a breakpoint in there will allow you to modify the value of no_skip while you're on the breakpoint, making the script not execute the next line.

Let me know if this helps.

Upon further looking it appears you're going to have to make sure your while loops are closed... so this might be a little more complicated if you're using if/while in the script... Sorry.