Inputs that you add with addRequired or addOptional are positional arguments. When you call a
function with positional inputs, specify those values in the order they are added to the parsing
scheme.
Inputs added with addParameter are not positional, so you can pass values for height before or
after values for width. However, parameter value inputs require that you pass the input name
(height or width) along with the value of the input.
If your function accepts optional input strings or character vectors and name-value arguments,
specify validation functions for the optional inputs. Otherwise, the Input Parser interprets the
optional strings or character vectors as parameter names. For example, the checkFinish validation
function ensures that printPhoto interprets 'glossy' as a value for finish and not as an invalid
parameter name.
Step 4. Set properties to adjust parsing (optional).
By default, the Input Parser makes assumptions about case sensitivity, function names, structure
array inputs, and whether to allow additional parameter names and values that are not in the scheme.
Properties allow you to explicitly define the behavior. Set properties using dot notation, similar to
assigning values to a structure array.
Allow printPhoto to accept additional parameter value inputs that do not match the input scheme
by setting the KeepUnmatched property of the Input Parser.
p.KeepUnmatched = true;
If KeepUnmatched is false (default), the Input Parser issues an error when inputs do not match the
scheme.
Step 5. Parse the inputs.
Within your function, call the parse method. Pass the values of all of the function inputs.
parse(p,filename,varargin{:})
Step 6. Use the inputs in your function.
Access parsed inputs using these properties of the inputParser object:
• Results — Structure array with names and values of all inputs in the scheme.
• Unmatched — Structure array with parameter names and values that are passed to the function,
but are not in the scheme (when KeepUnmatched is true).
• UsingDefaults — Cell array with names of optional inputs that are assigned their default values
because they are not passed to the function.
Within the printPhoto function, display the values for some of the inputs:
disp(['File name: ',p.Results.filename])
disp(['Finish: ', p.Results.finish])
if ~isempty(fieldnames(p.Unmatched))
disp('Extra inputs:')
disp(p.Unmatched)
end
if ~isempty(p.UsingDefaults)
disp('Using defaults: ')
21 Function Arguments
21-14
Précédent

- 770/1450

Suivant