Switch Case where case is a word_anynumber

조회 수: 3 (최근 30일)
Jonathan Crider
Jonathan Crider 2011년 11월 14일
I am trying to make a switch case where the case is satisfied by the following:
word_anynumber (ie word_1 or word_2 etc)
what do i do to achieve this?
The issue is that i want the case to do the same thing for any case with word_#, but i don't want to create cases for each number because i have a variable number of numbers each time this script runs.
Any help would be greatly appreciated.
-Jonathan

채택된 답변

Daniel Shub
Daniel Shub 2011년 11월 15일
A regular expression might be useful for this type of task. Using the following as the test case:
a = cellstr([repmat('word_', 100, 1), num2str((0:99)', '%02d')]);
b = cellstr([repmat('Word_', 100, 1), num2str((0:99)', '%02d')]);
c = cellstr([repmat('xword_', 100, 1), num2str((0:99)', '%02d')]);
x = [a;b;c];
The following:
~cellfun(@isempty, regexp(x, 'word_[0-99]', 'matchcase'));
will return true for any x that is word_# and fail on case changes. How to build your case/switch around this is not clear (it seems like an if/elseif/else problem to me.
  댓글 수: 1
Jonathan Crider
Jonathan Crider 2011년 11월 15일
Thank you for your help. I ended up switching from a switch case to a if/elseif and used the regexp(x, 'word_[0-99]', 'matchcase') expression and it is working. This will be good for now, but i may revisit this later for speed reasons. Thank you all for your help.
-Jon

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Fangjun Jiang
Fangjun Jiang 2011년 11월 14일
Use strfind(), strcmp() or strmatch(). You may also consider use "if-else" rather than 'case-switch"
if strfind(StrVariable,'word_')
  댓글 수: 1
Daniel Shub
Daniel Shub 2011년 11월 15일
I think this will evaluate to true if StrVariable is of the form * word_*. It doesn't require word to be the first element, numbers to follow the underscore, or the numbers to be the final elements.

댓글을 달려면 로그인하십시오.


Jonathan
Jonathan 2011년 11월 14일
If all of your cases are similar to what you describe, then you can use strtok like this.
a = 'word_4';
b = 'word_27';
c = 'byte_3';
d = 'bit_14';
switchStr = a;
switch strtok(switchStr, '_')
case 'word'
disp('word')
case 'byte'
disp('byte')
case 'bit'
disp('bit')
otherwise
disp('otherwise')
end
This displays 'word'. Try repeating this with switchStr set to b, c, and d.
  댓글 수: 6
Jonathan
Jonathan 2011년 11월 15일
strtok does work in this case since "strtok('apple', '_')" returns the string "apple".
Jonathan
Jonathan 2011년 11월 15일
a = 'apple';
b = 'orange';
c = 'banana';
d = 'grape_1';
e = 'grape_2';
f = 'grape_3';
switchStr = d; % goes through a through f
switch strtok(switchStr, '_')
case 'apple'
disp('apple')
case 'orange'
disp('orange')
case 'banana'
disp('banana')
case 'grape' % this is where i have a problem
disp('grapes')
end

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by