I'm trying to develop a function that contains multiple arguments. To be as robust as possible, I want to be able to call my function as follows:
foo( x, y, z, 'OptionalArg1', bar, 'OptionalArg2', blah, 'OptionalArg3', val )
I want my function to be robust enough to contain any combination of these arguments in any order. I also need to be able to set defaults if the argument is not provided. Is there a standard way to do this in MATLAB?
The best way would be to use the
inputParserclass, with theaddParametersfunction.In short, your code would look like:
Alternatively, you could write your own as I did (example here). The code there is easily modifiable to have your own input parser (you just need to change the
opts, and add aswitchfor each newopt.But the
inputParseris easier, and clearer to use.