Clear Filters
Clear Filters

switch with "ignore case" -> switchi

14 views (last 30 days)
Thomas Becker
Thomas Becker on 12 Jul 2017
Commented: Jan on 12 Jul 2017
Just a question or a proposal for an enhancement:
Has anyone ever wondered, why there isn't an enhanced switch-function for ignoring the case of strings, like strcmp/strcmpi, regexp/regexpi. It would be just consistent to have a switchi function.
Currently, you always have to program this problem using switch lower(xxx) and change all cases to lower letters..

Accepted Answer

Jan
Jan on 12 Jul 2017
Edited: Jan on 12 Jul 2017
switch can operate on numerical data also. Then a "switchi" is not meaningful and would lead to ambiguities:
if rand < 0.5
x = 'Hello';
else
x = 9;
end
switch x
case 'Hello', disp('hello');
case 9, disp('9');
end
In my opinion switch lower(x)) is clear and clean. If x is created anywhere far apart in the code, "switchi" would be misleading, because it requires additional assumptions about the contents of the argument.
By the way: The public forum is not the right location to post an enhancement request. Use the official MathWorks account instead, see the "Contact Us" link on this page.
  1 Comment
Thomas Becker
Thomas Becker on 12 Jul 2017
Hi Jan,
when reading your answer, this seems to be the reasons for not having a switchi command. Thanks a lot for your very fast answer.

Sign in to comment.

More Answers (1)

Walter Roberson
Walter Roberson on 12 Jul 2017
Edited: Walter Roberson on 12 Jul 2017
The case labels are technically expressions rather than constants, so you can also wrap them in lower() calls instead of changing their content.
On the other hand, if you go into Preferences to Keyboard => Shortcuts, you can add a shortcut for "Change to Lower Case", which you would probably use by highlighting the text and giving the shortcut sequence. That might be easier than typing lower() around each of the labels.
I find that in practice I do not tend to use switch() very often, and certainly not with enough cases to make changing the labels to lowercase to be an issue. Instead I find that in the circumstances where someone might typically use a switch(), that I instead use data structures that contain both the text and the control information about what to do, and then it is a matter of applying a small analysis function to figure out the offset into the table and pulling out the control information and executing it.
  3 Comments
Thomas Becker
Thomas Becker on 12 Jul 2017
Thanks for your hint for using more shortcuts - always appreciated!
I don't use switch too often, either, but a collegue does very frequently, and he asked me for tricks to simplify it. Good idea with using data structures instead!
Jan
Jan on 12 Jul 2017
A switch with a bunch of cases is nicer than a stack of elseif branches:
switch lower(Command)
case 'start'
case 'stop'
case 'forget'
case 'relax'
otherwise % No SWITCH without OTHERWISE!
error('Programming error: Unexpected Switch expression!');
end
if strcmpi(Command, 'start')
elseif strcmpi(Command, 'stop')
elseif strcmpi(Command, 'forget')
elseif strcmpi(Command, 'relax')
else % No ELSEIF stack without ELSE!
error('Programming error: Unexpected ELSEIF branch!');
end
With switch you have a list with arguments, with elseif a list of commands for the comparisons. Therefore I think that switch is leaner.
Using a data structure instead is useful, if the list of commands is expanded dynamically and if a look-up-table is applicable at all.

Sign in to comment.

Categories

Find more on MATLAB in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!