Display the error containing line in .m file

Is there a method to display the error containing line in .m file, when the execution stops?

1 件のコメント

Oleg Komarov
Oleg Komarov 2011 年 8 月 2 日
Your question sounds like:
How to pinpoint the line that errors in a "highlight some lines + F9"?

サインインしてコメントする。

回答 (3 件)

Jan
Jan 2011 年 8 月 2 日

2 投票

Perhaps you are looking for:
dbstop if error
or if you want to catch error inside TRY-CATCH also:
dbstop if all error
[EDITED]: In modern Matlab versions this is documented:
dbstop if caught error
Then the failing line is highlighted in the editor automatically.

8 件のコメント

Oleg Komarov
Oleg Komarov 2011 年 8 月 2 日
Didn't know about all error!
Jan
Jan 2011 年 8 月 2 日
@Oleg: It was documented in Matlab 6.5. I think it is equivalent to the current "dbstop if caught error".
Oleg Komarov
Oleg Komarov 2011 年 8 月 2 日
@Jan: actually it's not.
Seems like "dbstop if all error" is not documented anymore and it's funny how dbclear refers to "if error all".
Jan
Jan 2011 年 8 月 2 日
@Oleg: I get in Matlab 2009a:
dbclear('all'); dbstop('if', 'all', 'error'); dbstatus
>> Stop if error, Stop if caught error.
Therefore I assume "dbstop if all error" is just a synonym.
Oleg Komarov
Oleg Komarov 2011 年 8 月 2 日
@Jan: 2011a http://www.mathworks.com/help/techdoc/ref/dbstop.html
'caught error' only for try-catch and it doesn't display the line if a simple highlight execute is performed on a script.
Horia
Horia 2011 年 8 月 2 日
R2011a:
dbstop in file
dbstop in file at location
dbstop in file if expression
dbstop in file at location if expression
dbstop if condition
dbstop(s)
Mathworks seems to love play "hide and seek", until they come with another release...
Oleg Komarov
Oleg Komarov 2011 年 8 月 2 日
Anyways: 'dbstop if all error' works nevertheles on 2011a
Horia
Horia 2011 年 8 月 3 日
But no longer appearing in "Help".

サインインしてコメントする。

Fangjun Jiang
Fangjun Jiang 2011 年 8 月 2 日

0 投票

It is usually indicated in the error message.
Also, help dbstack.

5 件のコメント

Horia
Horia 2011 年 8 月 2 日
The error message indicates the line in the function associated with the line in the .m file, not the .m file line resoinsuble for the error.
Walter Roberson
Walter Roberson 2011 年 8 月 2 日
How do you define which line of code is "responsible" for a logic error that doesn't trigger an explicit fault for another several days?
Walter Roberson
Walter Roberson 2011 年 8 月 2 日
The standard error message traceback shows not only any chain of library calls, but also the line in the user .m file
Jan
Jan 2011 年 8 月 3 日
Under which circumstances does it (MATLAB?) show "error at" without a line number? Usually a line number is following.
Horia
Horia 2011 年 8 月 3 日
The line number in the function or library used by Matlab, but not the one in the .m file.

サインインしてコメントする。

Paulo Silva
Paulo Silva 2011 年 8 月 2 日

0 投票

After you get the error, execute this
l=lasterror;
fprintf('The last error was on line %d of the file called %s \n',...
l.stack.line,l.stack.name)

1 件のコメント

Oleg Komarov
Oleg Komarov 2011 年 8 月 2 日
I get an empty stack if in a new .m file:
s = s(1)
l = lasterror

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeDebugging and Analysis についてさらに検索

質問済み:

2011 年 8 月 2 日

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by