Switch/case in cell array of strings

6 ビュー (過去 30 日間)
Giorgos Papakonstantinou
Giorgos Papakonstantinou 2013 年 6 月 1 日
I have cell array of stings which I call it choice. The variable choice can take the following values:
choice={'Yes','Yes'};
choice={'Yes','No'};
choice={'No','Yes'};
choice={'No','No'};
I am using the switch/case to do other certain calculations. So..
switch choice
case {'Yes','Yes'}
...
case {'Yes','No'}
...
case {'No','Yes'}
...
case {'No','No'}
...
end
But switch/case gives an error:
SWITCH expression must be a scalar or string constant.
In the documentation I find:
case {'pie','pie3'}
Why then I get an error?

回答 (3 件)

Azzi Abdelmalek
Azzi Abdelmalek 2013 年 6 月 1 日
編集済み: Azzi Abdelmalek 2013 年 6 月 1 日
choice='Yes'
switch choice
case {'Yes','Yes'}
y=1
case {'Yes','No'}
y=2
case {'No','Yes'}
y=3
case {'No','No'}
y=4
otherwise
y=100
end
%Where is the problem?
Note that case {'Yes','No'} and case{'No','Yes') are the same.And case {'Yes','Yes'} is equivalent to case 'Yes'
  3 件のコメント
Giorgos Papakonstantinou
Giorgos Papakonstantinou 2013 年 6 月 1 日
i didn't know that {'Yes','No'} and case{'No','Yes') were the same
Walter Roberson
Walter Roberson 2013 年 6 月 1 日
in a case label, the order of appearance of the values does not matter. Think of it as doing an ismember(expression, labels) and executing the case if it is true.

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


Walter Roberson
Walter Roberson 2013 年 6 月 1 日
An evaluated switch_expression is a scalar or string. An evaluated case_expression is a scalar, a string, or a cell array of scalars or strings.
Your switch expression is a cell array, not a scalar or string, so you get the error.
You may wish to use isequal() such as
isequal(choice, {'Yes', 'Yes'})
or you might want to
switch [choice{1} '/' choice{2}]
case 'Yes/Yes'
case 'Yes/No'
end
There are computational alternatives for the case of a fixed finite number of entries in the cell, each with a fixed finite set of possibilities, but once you get into that, you are probably better off going table-driven instead of using switch()
  2 件のコメント
Giorgos Papakonstantinou
Giorgos Papakonstantinou 2013 年 6 月 1 日
What do you mean by "going table driven"? Can you give an example of a computational alternative?
Walter Roberson
Walter Roberson 2013 年 6 月 1 日
Computational:
[tf, idx] = ismember(choice, {'Yes', 'No'});
switch (idx(1)-1) * 2 + (idx(2)-1)
case 0 %yes yes
case 1 %yes no
case 2 %no yes
case 3 %no no
end
Table driven:
choice_routines = {@() disp('Yes/Yes'), @() disp('Yes/No'), @() disp('No/Yes'), @() disp('No/No') };
[tf, idx] = ismember(choice, {'Yes', 'No'});
offset = (idx(1)-1) * 2 + (idx(2)-1) + 1;
choice_routines{offset}(); %table lookup and call it

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


Giorgos Papakonstantinou
Giorgos Papakonstantinou 2013 年 6 月 1 日
Thank you. I wasn't using it properly. I thought that it evaluated the whole cell array strings.

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by