gpt4 book ai didi

matlab - matlab-抑制错误消息回溯

转载 作者:行者123 更新时间:2023-12-03 07:42:02 25 4
gpt4 key购买 nike

我尝试检查输入参数是否为特定类型,并抛出如下错误消息:

function test(input)
if ~ischar(input)
error('%s is invalid input type.', class(input));
end
end

但是Matlab使用 backtrace -information显示错误消息:
>> test(1)
Error using test (line 3)
double is invalid input type.

如何关闭 Error using test (line 3)行?

我正在寻找类似于 off backtracewarning的东西: warning off backtrace;

最佳答案

我不确定你能做到。我得到的最接近的是通过定义自己的错误结构:

testerr.message = 'test';
testerr.identifier = '';
testerr.stack.file = '';
testerr.stack.name = 'Test Thing';
testerr.stack.line = 1;

error(testerr)

哪个返回:
Error using Test Thing
test

只要将 file字段保留为空白,它就不会显示堆栈中指定的行。

一种可能的解决方法是 Undocumented MATLAB的结合使用 fprintfreturn:
function test(input)
if ~ischar(input)
fprintf(2, '%s is invalid input type.\n', class(input));
return
end
end

根据此检查在您的实际函数中的位置,您可能需要在如何退出方面变得很有创意,因为 return仅使您回到调用函数的位置。它可能输出了 True/ False标志吗?

关于matlab - matlab-抑制错误消息回溯,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31149656/

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