parse
Method of inputParser that matches a call's arguments against the declared parameters.
Category: Programming > Functions
Syntax
parse(p, varargin)
Parses the arguments and stores the results in p.Results.
Description
parse assigns the supplied values to the declared names, checks each with its validation function, and fills in defaults for anything not given. After the call p.Results is a structure with one field per declared argument, p.UsingDefaults lists the names that took their default, and p.Unmatched holds name-value pairs that were not declared when KeepUnmatched is true.
If a required argument is missing the call fails with Not enough input arguments. If a validator rejects a value, the message names the argument and the function that failed.
Input arguments
p- An
inputParserobject on whichaddRequired,addOptional,addParameteroraddParamValuewere already called. varargin- The values to parse: required ones first, then optional ones, then name-value pairs.
Example
Required, optional and name-value arguments
p = inputParser;
addRequired(p, 'x');
addOptional(p, 'n', 5);
addParameter(p, 'mode', 'fast');
parse(p, 10, 'mode', 'slow');
r = p.Results
p.UsingDefaults
Output
r = struct with fields:
mode: 'slow'
n: 5
x: 10
ans = 1×1 cell
{'n'}