Dohyun
Dohyun

Reputation: 642

Various input for one function in matlab

I know that I can have various input number by using varargin.

However, if you type sparse( in MATLAB, you can see that the input argument names are different for the number of input arguments.

various input arguments

I want to make such input style.

Is it possible to make such function? or is it only limitted to built-in function?

Upvotes: 0

Views: 48

Answers (1)

Mohammadreza Khoshbin
Mohammadreza Khoshbin

Reputation: 329

You can do this using the inputParser class and the addOptional method. Basically, MATLAB checks for the argument and if it does not exist in the function call, assigns a default value to it.

From docs:

addOptional(p,argName,default) adds optional input, argName, to the input parser scheme of inputParser object, p. When the inputs that you are checking do not include a value for this optional input, the input parser assigns the default value to the input.

addOptional(p,argName,default,validationFcn) specifies a validation function for the input argument.

See docs for inputParser and addOptional for examples.

Upvotes: 1

Related Questions