MATLAB: Switch Case where case is a word_anynumber

switch case string compare

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

Best Answer

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.
Related Question