MATLAB Programming Fundamentals - MathWorks

(やまだぃちぅ) #1

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: ')
disp(p.UsingDefaults)
end


Step 7. Call your function.


The Input Parser expects to receive inputs as follows:



  • Required inputs first, in the order they are added to the parsing scheme with
    addRequired.

  • Optional positional inputs in the order they are added to the scheme with
    addOptional.

  • Positional inputs before parameter name and value pair inputs.

  • Parameter names and values in the form Name1,Value1,...,NameN,ValueN.


Pass several combinations of inputs to printPhoto, some valid and some invalid:


Parse Function Inputs
Free download pdf