Effacer les filtres
Effacer les filtres

combining multiple or statements

3 vues (au cours des 30 derniers jours)
shru s
shru s le 12 Juil 2017
Commenté : shru s le 12 Juil 2017
hello, i wish to combine this or statement in in one line
switch m
case m==1|2|3|4|5|6|7|8|9|10
disp('A');
end
Is this possible? I do not want to type 1 2 3 4 etc because i have a lot of values to type in. Thank you

Réponse acceptée

Rik
Rik le 12 Juil 2017
As the documentation for switch suggests, making a cell array enables you to check multiple conditions at once. You can open the documentation by typing doc switch or by using Google.
m=3;
switch m
case {1,2,3,4}
disp('boo!')
otherwise
disp('ah!!')
end
You can also use a variable instead of typing them out.
m=3;
c=num2cell(1:10);
switch m
case c
disp('boo!')
otherwise
disp('aw..')
end
  2 commentaires
shru s
shru s le 12 Juil 2017
Brilliant! Thank you so much! :) if i run it for three loops and i get boo! boo! aw.. is the 1st 2nd and 3rd loop respectively, is there a way i can concatenate them together and display them? like boo!boo!aw..
shru s
shru s le 12 Juil 2017
ive got it. thank you :)

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by