switch with "ignore case" -> switchi

22 visualizaciones (últimos 30 días)
Thomas Becker
Thomas Becker el 12 de Jul. de 2017
Comentada: Jan el 12 de Jul. de 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..

Respuesta aceptada

Jan
Jan el 12 de Jul. de 2017
Editada: Jan el 12 de Jul. de 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 comentario
Thomas Becker
Thomas Becker el 12 de Jul. de 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.

Iniciar sesión para comentar.

Más respuestas (1)

Walter Roberson
Walter Roberson el 12 de Jul. de 2017
Editada: Walter Roberson el 12 de Jul. de 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 comentarios
Thomas Becker
Thomas Becker el 12 de Jul. de 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 el 12 de Jul. de 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.

Iniciar sesión para comentar.

Categorías

Más información sobre Matrix Indexing en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by