在Matlab中打印行号

时间:2013-05-09 02:47:20

标签: matlab debugging

我有一个Matlab函数,它可以运行几千行代码。在一定条件下,它正在破碎。我也可以调试代码并逐步运行。

所以,我已经尝试在Matlab中捕获块来处理错误。除此之外,还可以捕获代码的行号。

例如:

try
 Error here <-----
catch err
disp(['Error occured on line No ' num2str(lineNo])
end

任何想法,如何实施?

4 个答案:

答案 0 :(得分:4)

试试这个。这将打印出行号以及完整堆栈。

try
    %some code;
catch exc
    getReport(exc, 'extended')
end

答案 1 :(得分:2)

您也可以考虑使用

>> dbstop if error

在运行代码之前:这样当发生错误时,Matlab会创建一个调试断点并允许您在错误时进行调试。

答案 2 :(得分:0)

您可以尝试这种方式:

try
  Error here <--------------
catch err
    disp([err.identifier]);
    disp([err.message]);
    for e=1:length(err.stack)
       disp(['Error in ' err.stack(e).file ' at line ' num2str(err.stack(e).line)]); 
    end
end

答案 3 :(得分:0)

要打印行号,您可以使用此命令:

printf(['Line number ' num2str(dbstack.line) '\n'])