if any command to check table values

17 ビュー (過去 30 日間)
Ilyass Boukhari
Ilyass Boukhari 2021 年 1 月 17 日
編集済み: dpb 2021 年 1 月 17 日
I have a problem in checking if a table contains at least one value above a set limit.
I am using this short code
if any(Y>Threshold) %conditions
disp('ExtinguishMode ON')
else disp('ExtinguishMode OFF')
end
Even though when checking the Y variable table there are numbers above the threshold (which is 100), the command still displays the opposite condition 'ExtinguishMode OFF'. I am attaching here a screenshot
What am I doing wrong?

採用された回答

Adam Danz
Adam Danz 2021 年 1 月 17 日
編集済み: Adam Danz 2021 年 1 月 17 日
Assuming Y is a matrix,
if any(Y>Threshold,'all')
Conditional statements expect to recieve a scalar value. Your syntax was providing a vector and if any values of the vector are false, the condition will be false.
  1 件のコメント
Ilyass Boukhari
Ilyass Boukhari 2021 年 1 月 17 日
Thank you, that worked

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

その他の回答 (1 件)

dpb
dpb 2021 年 1 月 17 日
編集済み: dpb 2021 年 1 月 17 日
We don't have the data and can't see the actual result in context -- although the code snippet posted isn't formatted well, that doesn't affect the outcome;
if any(Y>Threshold) %conditions
disp('ExtinguishMode ON')
else disp('ExtinguishMode OFF')
end
is the same as
if any(Y>Threshold) %conditions
disp('ExtinguishMode ON')
else
disp('ExtinguishMode OFF')
end
BUT, the problem is that if() is TRUE iff all elements of the result are TRUE but since Y is a 2D array, the logical expression any(Y>Threshold) is a row vector of the conditions by column. Apparently there is at least one column for which the threshold is not exceeded.
You're looking for the optional 'all' flag to any here...
if any(Y>Threshold,'all')
If that is not recognized in your release (not sure when that syntax was introduced), then use
if any(Y>Threshold(:)) % check all of Threshold as vector
or
if any(any(Y>Threshold)) % check all of Threshold by checking vector returned by any

カテゴリ

Help Center および File ExchangeNumeric Types についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by