How to add only two choices for an input parameter ?

I would like a function to accept only 'string1' or 'string2' for param3 i.e.:
output = fun(param1, param2, param3)
What should I add to param3 ?
Thank you

 Akzeptierte Antwort

Ameer Hamza
Ameer Hamza am 5 Mai 2018
Use assert() to throw error if condition fail
assert(any(strcmp(param3, {'string1', 'string2'})), 'param3 must b string1 or string2')
it will compare param3 to see whether it is 'string1' or 'string2'. If none has detected the function with end an throw an error.

Weitere Antworten (1)

John D'Errico
John D'Errico am 5 Mai 2018

0 Stimmen

You cannot "add" something to param3.
You CAN test to see if param3 is anything else, and then return an error.

Kategorien

Mehr zu Characters and Strings finden Sie in Hilfe-Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by