在 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
我当然愿意承受人们可能期望的性能损失.
I would of course be willing to take the performance hit that one may expect.
推荐答案
使用 dbstop
的DBSTOP in FILESPEC if EXPRESSION"形式的问题是它只在 第一行文件.一种解决方案是使用DBSTOP in FILESPEC at 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屋!