Catch terminal's error message

6 ビュー (過去 30 日間)
Juan Ramirez
Juan Ramirez 2018 年 5 月 15 日
コメント済み: Juan Ramirez 2018 年 5 月 15 日
I have a long script executed remotely and would like to capture any error in a log file. I would prefer not to write several try/catch statements, and would rather write a single try/catch around the entire function, and record the exact error, with a line number. But I can't find a tool to capture this.
Even in this simple example, I cannot capture the error message, and the program does not fail gracefully:
try
1=0;
catch ME
disp(ME.identifier)
end
The terminal says
Error: File: test.m Line: 2 Column 6
Incorrect use of '=' operator. To assign a value...
It is this message that I want saved and directed to a file. Can it be done?

採用された回答

Guillaume
Guillaume 2018 年 5 月 15 日
As Walter says, your example script will never execute because of the syntax error so try catch inside the file is no good.
However, a try... catch statement in the caller of the file would trap the error. So:
try
nameoflongscript %invoke long script
catch ME
disp(ME)
end
would work.
Alternatively, to detect such parse errors before actually running the script, you could use mlint to analyse the file, or the undocumented mtree to get the parse tree of the file. Either of these will tell you if there are any parse error.
  1 件のコメント
Juan Ramirez
Juan Ramirez 2018 年 5 月 15 日
Thank you both! Using a try...catch as in above is exactly what I wanted.

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

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2018 年 5 月 15 日
No, in all modern MATLAB versions, the file is completely parsed before any of it is executed. You would need to have the try/catch in the calling code.

カテゴリ

Help Center および File ExchangeEntering Commands についてさらに検索

製品


リリース

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by