Matlab调试:跳过下一行而不执行 [英] Matlab debug: skip next line without execution

查看:1525
本文介绍了Matlab调试:跳过下一行而不执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题:
(问题的完整说明如下)




  • 有没有人有建议如何把Matlab跳到一行或几行代码? (mex / java /重写一些内部的Matlab功能?)

  • 有没有人知道哪里可以找到代码文件(如果存在的话) db * )?



在调试/运行程序时,Matlab中有几个函数允许流控制: dbstop dbcont dbstep 等等...我试图找到一个 db * 函数可能会跳过Matlab脚本中的下一行, dbskip 也许,但遗憾的是没有这样的内置函数,它没有似乎这样功能将被实施(10年计数)



编辑使用示例:



以下脚本 myScript 可以调用函数 myFunc ,它决定是否根据输入的值跳过调用者脚本中的下一行,如下所示:

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

%myFunc.m
函数myFunc(u)
如果u == 1
%跳过下一行在调用者 - 如何?
%...做更多的东西...
else
%...做其他的东西...
disp('像往常一样')
结束
%...做更多的东西...
结束

编辑



正如@迈克尔·史密斯所建议的,实现这一点的一种方法是编写一个程序,它将读取 myScript .m 并逐行跳过在运行时指定的任何行。我相信这个解决方案是一个很好的开始;然而,在更复杂的情况下,函数调用中将会有函数调用。所以,使用这种方法做一个真正的一般解决方案,需要编写一个完整的Matlab代码解释器,具有Matlab所具有的所有功能以及我自己的一些要求。这是我想避免的,如果我可以使用一些Matlab的内在反射能力或黑客入侵调试系统。



同时,我碰到另一个墙,试图找到如何以及在哪里实现 dbstep 。调试任何 db * 函数时,结果是以下错误:

 使用< db *> 
仅在调试模式下停止时才允许调试命令。

只有 dbstep.m find,包含没有任何代码的文档,所以我假设它是一个内置的编译器。

解决方案

好的,这可能会有点混乱。我可以想到两种可能的方法来做到这一点。其中一个涉及编辑脚本,让每一行都有一个if语句来查看可以在工作区中修改的局部变量。我要打电话给这个。所以让我们看看下一个想法。



matlab有一个命令eval(EXPRESSION),它接受一个字符串并执行。这是想法让我们创建一个脚本/函数进行调试,如下所示:

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

executed_script = 1;
while(executed_script == 1)
line_no%打印行号
line = fgetl(FID)%删除;所以它会写行
if(no_skip)
eval(line)
end
line_no = line_no + 1;
end

有一个断点将允许你修改no_skip的值,而你'在断点上,使脚本不执行下一行。



让我知道这是否有帮助。



进一步看,看来你将不得不确保你的while循环是关闭的...所以这可能会有点复杂,如果你在脚本中使用if / while ...对不起, p>

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.

解决方案

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.

这篇关于Matlab调试:跳过下一行而不执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆