我尝试检查输入参数是否为特定类型并抛出错误消息,例如:
function test(input)
if ~ischar(input)
error('%s is invalid input type.', class(input));
end
end
但是 Matlab 显示带有 backtrace
的错误消息 - 信息:
>> test(1)
Error using test (line 3)
double is invalid input type.
如何关闭Error using test (line 3)
行?
我正在寻找类似于 off backtrace
和 warning
的内容: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
字段保留为空,它就不会显示堆栈中指定的行。
一个潜在的解决方法可能是结合使用 fprintf
和 return
,由 Undocumented MATLAB 提供。 :
function test(input)
if ~ischar(input)
fprintf(2, '%s is invalid input type.\n', class(input));
return
end
end
根据此检查在实际函数中的位置,您可能需要对其退出方式进行创意,因为 return
只会将您踢回调用函数。可能让它输出一个 True
/False
标志?
关于matlab - 抑制错误消息回溯,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31149656/