do specific work after a specific warning

1 回表示 (過去 30 日間)
civil tech
civil tech 2022 年 7 月 4 日
コメント済み: civil tech 2022 年 7 月 5 日
Hello everyone.
I want to write a command with the following task:
if a specific warning is happend, do a specific work.
For example:
class 0 = [1, 2, 2, 1, 4, 5, 4, 4,];
warning('off');
class = kmeans(X,10);
if (Warning: Failed to converge in 100 iterations during replicate 10)
class = class0;
end
How to write the "if-condition" above in the command line?

回答 (2 件)

Walter Roberson
Walter Roberson 2022 年 7 月 4 日
Use lastwarn, especially the two-output version that lets you get the message id.
Trick of the trade, though: immediately before the command to be tracked, use
warning('')
otherwise lastwarn might refer to a warning from before the kmeans call.
  1 件のコメント
civil tech
civil tech 2022 年 7 月 5 日
Thank you.

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


Image Analyst
Image Analyst 2022 年 7 月 4 日
Maybe try this:
% Clear the last warning.
lastwarn('Success');
class = kmeans(X,10);
% See if there was a warning.
[warningMessageString, warningMessageID] = lastwarn;
if contains(warningMessageString, 'Failed to converge')
% Alert user
uiwait(warndlg(warningMessageString));
% Do anything else you want to do, like exit or whatever.
end
  1 件のコメント
civil tech
civil tech 2022 年 7 月 5 日
Thank you.

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

カテゴリ

Help Center および File ExchangeDebugging and Analysis についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by