using switch, case and comparing strings

140 visualizaciones (últimos 30 días)
Keith Holmlund
Keith Holmlund el 21 de Jun. de 2018
Comentada: belhassan haj mansour el 9 de En. de 2019
I am using a function with varargin that imports strings and numbers but the first input is always a string. based off what it says, using switch and case, I want the function to do something different. So if for example, str = char(varargin{1}) = 'hot dog' and I had
switch str
case str == 'hot dog'
case str == 'burger'
When I do this I get an error, what am I doing wrong
  2 comentarios
dpb
dpb el 21 de Jun. de 2018
Editada: dpb el 21 de Jun. de 2018
str = varargin{1} = 'hot dog'
is illegal syntax and the SWITCH construct is missing any action statements in the cases plus isn't closed.
A properly formed SWITCH construct will work just fine with string or character variables; it will have an issue with a cellstr() argument unless you take care to dereference it.
We need to see the actual function to debug, not just bits and pieces.
Keith Holmlund
Keith Holmlund el 21 de Jun. de 2018
Editada: Keith Holmlund el 21 de Jun. de 2018
It looks something like this, The first input is 'hot dog'. When run I enter Menu('hot dog','ketchup'). I am getting an error at the first case line
function Menu(varargin)
cat = char(varargin{1});
switch cat
case cat == 'hot dog'
condiment = char(varargin{2});
subfunction
case cat == 'burger'
bun = char(varargin{2});
subfunction2
end

Iniciar sesión para comentar.

Respuesta aceptada

OCDER
OCDER el 21 de Jun. de 2018
Editada: OCDER el 21 de Jun. de 2018
The switch statement is used incorrectly. Remove the "str ==" after case.
function Menu(varargin)
%varargin = {'hot dog', 1, 2, 3};
str = varargin{1};
switch lower(str) %make it case insensitive. all lower cases
case 'hot dog'
disp('Delicious hot dogs!')
case 'burger'
disp('Mouth-watering burgers!')
end
Actually, what was the Error message? I should have waited for your response to @dpb since there are multiple issues, as already pointed out.
  3 comentarios
OCDER
OCDER el 21 de Jun. de 2018
Editada: OCDER el 21 de Jun. de 2018
Remove 'cat == ' after your case. And DO NOT use "cat" as a variable, as that's a built-in matlab function for concatenate arrays.
help cat
I like to label my variables with 1st letter caps, just in case I accidentally name a variable the same as a matlab function.
Walter Roberson
Walter Roberson el 21 de Jun. de 2018
I tested with OCDER's code, and it looks fine to me.
OCDER's code does not use any string data types and so could not get that message.

Iniciar sesión para comentar.

Más respuestas (1)

belhassan haj mansour
belhassan haj mansour el 9 de En. de 2019
Editada: belhassan haj mansour el 9 de En. de 2019
HELLO
im trying to control a rocker switch with another one in app designer, for example i have rockerswitch1 in the position 'off' and rockerswitch2 also in position 'off' . what i need is when i put rockerswitch1 in position 'on', the rockerswitch 2 become in the position 'on' too but i can't find the right function to do so
CAN ANYONE HELP ME WITH THIS PLEASE ?
  2 comentarios
Walter Roberson
Walter Roberson el 9 de En. de 2019
Please create a new Question for this .
belhassan haj mansour
belhassan haj mansour el 9 de En. de 2019
done

Iniciar sesión para comentar.

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by