フィルターのクリア

Check if cell contains only certain combination of variables

2 ビュー (過去 30 日間)
Metin Akyol
Metin Akyol 2022 年 1 月 31 日
編集済み: Turlough Hughes 2022 年 1 月 31 日
I have a 4x1 cell filled with 4 possible string values: 'A', 'B', 'C', or 'D'
I would like to ensure that the values in there either:
any combination of: 'A' and/or 'B'
OR
any combination of 'C' and/or 'C'
But I want to raise an error if there is any mix between say A and C, or A and D, and so forth
  3 件のコメント
Metin Akyol
Metin Akyol 2022 年 1 月 31 日
So when I print out the cell, I get this output:
example_Cell = 4x1 cell
'A'
'B'
'C'
'A'
Metin Akyol
Metin Akyol 2022 年 1 月 31 日
So the above example, would thus be an invalid one based on my criterion, so this should raise a flag.

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

採用された回答

Turlough Hughes
Turlough Hughes 2022 年 1 月 31 日
編集済み: Turlough Hughes 2022 年 1 月 31 日
You can do this with an "exclusive or" (see xor) where you check that you have exlusively either 'A' or 'B' xor 'C' or 'D', but not a mix of A and C or A and D, etc. See the following:
x = {'A','B','C','D'}.';
% conditions
condA = any(matches(x,"A"|"B"));
condB = any(matches(x,"C"|"D"));
assert(xor(condA,condB),'Error: ...')
  1 件のコメント
Metin Akyol
Metin Akyol 2022 年 1 月 31 日
Fantastic, thank you so much. That's it.

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

その他の回答 (1 件)

Benjamin Thompson
Benjamin Thompson 2022 年 1 月 31 日
So something like this? I am sure you can add more detail to deal with all possible cases.
myCellArray{1} = 'A';
myCellArray{2} = 'B';
myCellArray{3} = 'C';
myCellArray{4} = 'D';
if (myCellArray{1} == 'A')
for (i = 2:4)
if ((myCellArray{i} == 'C') || (myCellArray{i} == 'D'))
disp('Not Good')
end
end
end
  1 件のコメント
Metin Akyol
Metin Akyol 2022 年 1 月 31 日
Yep, that is kind of how I did it too. Thank you so much.

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

カテゴリ

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

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by