Good practice in conditional test with categorical variable

So I defined a categorical variable and tested its value in a conditional statement using the "==" operator, as seen in the image. The program works OK, thus I'm guessing such syntax is acceptable. But I kept getting warnings like the one in the image in the MATLAB IDE. How can my code be changed so that the warning is suppressed, and what is the underlying explanation for what is and is not a good practice when dealing with categorical variables in a conditional test?

 採用された回答

the cyclist
the cyclist 2023 年 3 月 26 日
編集済み: the cyclist 2023 年 3 月 26 日

0 投票

The reason you are getting this mlint warning isn't really about the categorical variables at all. It is because you are trying to make a vector comparison in the if statement.
'def' is a 1x3 character vector, so MATLAB is warning you that it is expecting an expression that will evaluate to a scalar in the if statement. It is akin to writing
if [2 3] == [5 7 11]
Now, it turns out that because MATLAB will do a conversion to compare your categorical variable to the character array
catvar = categorical({'def'},{'abc','def'});
if catvar == 'def'
fprintf("got here\n")
end
got here
the syntax will work. (I don't know if it gives the result you intend, though.) In general, it's better not to rely on the conversion, but make it explicit. I don't know your broader context for wanting to use categorical variables overall, but this syntax would not give the warning.
catvar = categorical({'def'},{'abc','def'});
if catvar == categorical({'def'})
fprintf("got here, without mlint warning\n")
end
got here, without mlint warning
I do have the feeling that your whole situation might be easier if you used string arrays instead of categorical, but like I said, I don't know your broad context.

6 件のコメント

Xiaoxing Zhang
Xiaoxing Zhang 2023 年 3 月 27 日
編集済み: Xiaoxing Zhang 2023 年 3 月 27 日
Yes, this method does the trick, thanks. I just wonder why MATLAB introduced the kind of usage that would be frowned upon by mlint in their documentation in the first place. Then again, that's not my problem now. The categorical helps for vvvery large varibles with limited number of types, it saves memory comparing to string arrays and are more informative than, say, a int8 array.
the cyclist
the cyclist 2023 年 3 月 27 日
To be clear, the syntax
B == 'red'
is not what is throwing the warning. it is the use of that expression in the if statement
if B == 'red'
Xiaoxing Zhang
Xiaoxing Zhang 2023 年 3 月 27 日
Ah yes,I think I got your point, if I wrap all or any around the == statement it would be fine, so basically == for categoricals expects returning of an array. That does sounds in line with other MATLAB logics.
Thanks a lot!
Peter Perkins
Peter Perkins 2023 年 3 月 28 日
All true, but probably the best thing is
B == "red"
Strings make your life easier than cellstrs!
Xiaoxing Zhang
Xiaoxing Zhang 2023 年 3 月 29 日
Good point...
the cyclist
the cyclist 2023 年 3 月 29 日
Between @Peter Perkins and me, we probably have nearly 60 years of MATLAB experience, so if we both suggested using strings arrays, you know it's a good idea. :-)

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeGet Started with MATLAB についてさらに検索

製品

リリース

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by