获取在Matlab代码中执行的实际行 [英] Get actual line being executed in Matlab code

查看:264
本文介绍了获取在Matlab代码中执行的实际行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个问题困扰了我一段时间,我找不到答案.

I have this question which has been bugging me for some time and I could not find an answer to it.

我目前正在与一位同事一起研究很长的Matlab代码.我们每个人都在代码的不同部分上工作,而不会互相干扰.为了更好地使自己适应代码并遵循程序的流程,我放置了一些disp(),以便我知道代码何时到达某一行.

I'm currently working with a colleague on a Matlab code which is quite long. We each work on different parts of the code without interfering with one another. In order to better orient myself in the code and follow the flow of the program, I put a few disp() so that I know when the code reaches some line.

例如:

% code
    disp('You have reached line 1000')
% code...

但是,当我或我的同事在disp()命令上方添加/删除/修改代码行时,可能会很麻烦,以至于我在disp()中手动输入的行不再准确.

However it can be bothersome when I or my colleague add/remove/modify lines of code above the disp() command, so that the line I manually entered in the disp() is no longer accurate.

我的问题:是否可以在程序已到达的代码中打印实际的行号?这可能是一个非常幼稚的问题,但我想知道是否有可能.

My question: Is there a way to print the actual line number in the code that the program has reached? It might be a very naive question but I'd like to know if it is at all possible.

推荐答案

您可以检查dbstack.这是带有示例的文档,具体取决于您要打印的内容: http: //www.mathworks.co.uk/help/matlab/ref/dbstack.html

You can check dbstack. Here is the doc with examples, depending on what you want to print: http://www.mathworks.co.uk/help/matlab/ref/dbstack.html

简单示例,您可以将其放入文件debug_point.m:

Quick example, that you can put in a file debug_point.m:

%// Tested on Octave only, Matlab might be slightly different
function debug_point()
    d = dbstack(1);
    d = d(1);
    fprintf('Reached file %s, function="%s", line %i\n', d.file, d.name, d.line)
end

然后您可以调用此函数.您可以仅通过更改此功能来轻松关闭所有打印.

Then you can call this function. You can easilly turn off all the prints by changing this function only.

这篇关于获取在Matlab代码中执行的实际行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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