在MATLAB中自定义dbstop [英] Customize dbstop in MATLAB
问题描述
可以向Matlab添加自定义的 dbstop
条件吗?
Is it possible to add a customized dbstop
condition to Matlab?
最近我发现自己在多个变量中界定值,一种方法来跟踪这种情况的第一次出现将是在更新这些值的每一行上设置一个条件断点。然而,我希望有一个更简单的方法来做到这一点。
Recently I found myself with out of bounds values in multiple variables, one way to track down the first occurance of this would be to set a conditional breakpoint on each line where these values are updated. However, I hope there is an easier way to do this.
我最近不得不追踪一个 NaN
这是相当微不足道的,因为:
I have recently had to track down a NaN
which was fairly trivial due to:
dbstop if naninf
因此,我希望可以得到以下内容:
Hence I hope that it is possible to get something like:
dbstop if anything outside myBound
或
dbstop if myVariable outside myBound
我当然会愿意采取
推荐答案
在<$ c中使用DBSTOP在FILESPEC if EXPRESSION中的问题$ c> dbstop 是它只在文件的第一个行设置断点。一个解决方案是使用表单DBSTOP in FILESPEC in LINENO if EXPRESSION来设置每个行上的断点。
The problem with using the form "DBSTOP in FILESPEC if EXPRESSION" of dbstop
is that it sets a breakpoint only at the first line of the file. A solution is to use the form "DBSTOP in FILESPEC at LINENO if EXPRESSION" to set a breakpoint at each line.
考虑以下示例脚本,保存在名为 testfile.m
的文件中。
Consider the following example script, saved on a file called testfile.m
.
clear all
for m = 1:10;
k = 2*m
end
说如果变量 k
超过值 6
。我们首先在此文件的所有行中自动设置断点:
Say we want to stop if variable k
exceeds the value 6
. We first automatically set the breakpoints in all lines of this file:
file = 'testfile.m';
varname = 'k';
expression = 'k>6'; %// it should be 'exist(''k'')&&k>6', but that's added later
%// Determine number of lines of file:
fid = fopen('testfile.m');
cont = 1;
nlines = 0;
while cont
readline = fgetl(fid);
cont = ~isequal(readline,-1);
nlines = nlines + cont;
end
fclose(fid);
%// Set breakpoint at each line. We need eval for this
for n = 1:nlines
eval(['dbstop in ' file ' at ' num2str(n) ' if ( exist(''' varname...
''') && ( ' expression ' ) )'])
end
现在运行以上(检查 testfile.m
的每一行都有一个黄色的断点),运行 testfile
并在停止时检查值:
Now, after running the above (check that every line of testfile.m
has a yellow breakpoint), run testfile
and check values when it stops:
如果你有几个变量或文件,这肯定有点麻烦。此外,我不知道Matlab支持多少个并发断点(我们每个程序行都使用一个)。
This is admittedly a little cumbersome if you have several variables or files. Also, I'm not sure how many simultaneous breakpoints Matlab supports (we are using one for each program line).
这篇关于在MATLAB中自定义dbstop的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!