gpt4 book ai didi

matlab - 调用 fprintf 或 disp 时如何抛出错误?

转载 作者:太空宇宙 更新时间:2023-11-03 19:57:12 26 4
gpt4 key购买 nike

我有一个庞大的难以理解的代码库,其中写出了非常烦人的换行符。由于某些奇怪的原因,我似乎找不到它们在哪里。所以我在想,如果我能让 MATLAB 在调用 sprintfdisp 时抛出错误,我就能找到它们。有没有办法做到这一点?

我试过 evalc 但它给出的只是输出本身,而不是来自调用它的地方。

最佳答案

dbstop in

当然你可以重载dispfprintf ,或者您可以使用 dbstop 告诉调试器在这些函数处停止.然后使用 dbstack看看你在哪里以及你是如何到达那里的。没关系,这些是没有 MATLAB 代码的内置函数。它会在通话前停止:

>> dbstop in dispWarning: MATLAB debugger can only stop in MATLAB code files, and "libmwbuiltins>disp" is not a MATLAB code file.         Instead, the debugger will stop at the point right before "libmwbuiltins>disp" is called. >> dbstop in fprintfWarning: MATLAB debugger can only stop in MATLAB code files, and "libmwbuiltins>fprintf" is not a MATLAB code file.         Instead, the debugger will stop at the point right before "libmwbuiltins>fprintf" is called.

Looking good!

Consider the following test function testdbstop with a nested function fprintTest:

function testdbstop
x=1;
disp(x)
fprintTest(x)
function fprintTest(x)
fprintf('%d\n',x);
end
end

从命令行运行它:

>> testdbstop
3 disp(x)
K>> dbstack
> In testdbstop at 3
K>> dbcont
1
6 fprintf('%d\n',x);
K>> dbstack
> In testdbstop>fprintTest at 6
In testdbstop at 4
K>> dbcont
1

你知道了——在 testdbstop.m 的第 3 行调用了 disp , 和 fprintftestdbstop.m 的第 6 行调用在testdbstop>fprintTesttestdbstop 的第 4 行调用.

注意:完成后,使用 dbclear 删除虚拟断点(即 dbclear in dispdbclear in fprintf )。

关于matlab - 调用 fprintf 或 disp 时如何抛出错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21459713/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com