If statement with OR operator to create error message for a function

1 vue (au cours des 30 derniers jours)
LivingTheScienceDream
LivingTheScienceDream le 21 Oct 2013
Modifié(e) : Cedric le 21 Oct 2013
Hi
I have a function that has a second input that must be 8, 12 or 16. I want to have an error message to flag when the 2nd input does not take these values. I have tried doing this in an if statement:
if A~=8 || A~=12 || A~=16
error('..','...')
end
Of course, I think my logic here is wrong (if the input is 12, it is true for the A~=8 or 16) and so the if statement is always true and can never be false. Would an AND/OR work (if these exist in matlab)?
Is there a way I can do this in an if statement? Or is there a better way of writing what I'm trying to do?
Thanks for your help!

Réponse acceptée

Cedric
Cedric le 21 Oct 2013
Modifié(e) : Cedric le 21 Oct 2013
if A~=8 && A~=12 && A~=16
error('..','...') ;
end
you could also use ISMEMBER:
if ~ismember(A, [8, 12, 16])
error('..','...') ;
end

Plus de réponses (0)

Catégories

En savoir plus sur Signal Generation and Preprocessing dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by