Products & Services Solutions Academia Support User Community Company

Learn more about MATLAB   

parse (inputParser) - Parse and validate named inputs

Syntax

p.parse(arglist)
parse(p, arglist)

Description

p.parse(arglist) parses and validates the inputs named in arglist.

parse(p, arglist)is functionally the same as the syntax above.

For more information on the inputParser class, see Validating Inputs with inputParser in the MATLAB Programming Fundamentals documentation.

Examples

Write an M-file function called publish_ip, based on the MATLAB publish function, to illustrate the use of the inputParser class. Construct an instance of inputParser and assign it to variable p:

function publish_ip(script, varargin)
p = inputParser;   % Create an instance of the inputParser class.

Add arguments to the schema. See the reference pages for the addRequired, addOptional, and addParamValue methods for help with this:

p.addRequired('script', @ischar);
p.addOptional('format', 'html', ...
    @(x)any(strcmpi(x,{'html','ppt','xml','latex'})));
p.addParamValue('outputDir', pwd, @ischar);
p.addParamValue('maxHeight', [], @(x)x>0 && mod(x,1)==0);
p.addParamValue('maxWidth', [], @(x)x>0 && mod(x,1)==0);

Call the parse method of the object to read and validate each argument in the schema:

p.parse(script, varargin{:});

Execution of the parse method validates each argument and also builds a structure from the input arguments. The name of the structure is Results, which is accessible as a property of the object. To get the value of any input argument, type

p.Results.argname

Continuing with the publish_ip exercise, add the following lines to your M-file:

% Parse and validate all input arguments.
p.parse(script, varargin{:});

% Display the value for maxHeight.
disp(sprintf('\nThe maximum height is %d.\n', p.Results.maxHeight))

% Display all arguments.
disp 'List of all arguments:'
disp(p.Results)

When you call the program, MATLAB assigns those values you pass in the argument list to the appropriate fields of the Results structure. Save the M-file and execute it at the MATLAB command prompt with this command:

publish_ip('ipscript.m', 'ppt', 'outputDir', 'C:/matlab/test', ...
    'maxWidth', 500, 'maxHeight', 300);

The maximum height is 300.

List of all arguments:
              format: 'ppt'
           maxHeight: 300
            maxWidth: 500
           outputDir: 'C:/matlab/test'
              script: 'ipscript.m'

See Also

inputParser, addRequired(inputParser), addOptional(inputParser), addParamValue(inputParser), createCopy(inputParser)

  


Recommended Products

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.

 © 1984-2009- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS