addParameter
Adds a name-value argument to an inputParser.
Category: Programming > Functions
Syntax
addParameter(p, name, default)
Declares a name-value argument with a default.
addParameter(p, name, default, validationFcn)
Also validates the value.
Description
Name-value pairs can appear in any order after the positional arguments. Names are matched without regard to case (unless CaseSensitive is set) and a unique abbreviation is accepted while PartialMatching is on. A name that was never declared is an error, unless KeepUnmatched is true, in which case it is collected in p.Unmatched.
Input arguments
p- An
inputParserobject. name- Parameter name, as text. Callers write it in the call followed by its value.
default- Value used when the parameter is not given.
validationFcn- Handle to a function that returns
truefor a valid value, or errors.
Example
Parameters with defaults
p = inputParser;
addParameter(p, 'Tol', 1e-3, @(v) v > 0);
addParameter(p, 'Verbose', false);
parse(p, 'Tol', 0.5);
p.Results
Output
ans = struct with fields:
Tol: 0.5000
Verbose: 0