Info

This question is closed. Reopen it to edit or answer.

How do I get the element of a cell in a case_expression that matches the switch_expression?

2 views (last 30 days)
a = {'the', 'cat', 'is', 'the', 'dog'};
b = 'cat';
switch b
case a
end
%How would I know which element in a matches b? Would there be a way to get that element and use it?
  1 Comment
Stephen23
Stephen23 on 21 Oct 2015
Edited: Stephen23 on 21 Oct 2015
The answer to your question is no, there is no way to know which element of a matched b. If you need the index of the element, then you will have to generate this yourself using one of the methods given in the answers below.

Answers (2)

Thorsten
Thorsten on 21 Oct 2015
You can use
a(strcmp(a, b))

Stefan Raab
Stefan Raab on 21 Oct 2015
Edited: Stefan Raab on 21 Oct 2015
find(ismember(a,'cat'))
or
find(ismember(a,b))

Community Treasure Hunt

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

Start Hunting!