addParamValue (inputParser) - Add parameter-value argument to inputParser schema

Syntax

p.addParamValue(argname, default, validator)
addParamValue(p, argname, default, validator)

Description

p.addParamValue(argname, default, validator) updates the schema for inputParser object p by adding a parameter-value argument, argname. Specify the argument name in a string enclosed within single quotation marks. The default input specifies the value to use when the optional argument name is not present in the actual inputs to the function. The optional validator is a handle to a function that MATLAB uses during parsing to validate the input arguments. If the validator function returns false or errors, the parsing fails and MATLAB throws an error.

MATLAB parses parameter-value arguments after required arguments and optional arguments.

addParamValue(p, argname, default, validator) is functionally the same as the syntax above.

For more information on the inputParser class, see Parsing Inputs with inputParserin 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. There are three calling syntaxes for this function:

publish_ip('script')
publish_ip('script', 'format')
publish_ip('script', options)

From these calling syntaxes, you can see that there is one required argument (script), one optional argument (format), and a number of optional arguments that are specified as parameter-value pairs (options).

Begin writing the example publish_ip M-file by entering the following two statements. Call the class constructor for inputParser to create an instance of the class. This class instance, or object, gives you access to all of the methods and properties of the class:

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

After calling the constructor, add the following lines to the M-file. This code uses the addRequired(inputParser), addOptional(inputParser), and addParamValue methods to define the input arguments to the function:

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);

Also add the next two lines to the M-file. The Parameters property of inputParser lists all of the arguments that belong to the object p:

disp 'The input parameters for this program are
disp(p.Parameters)'

Save the M-file using the Save option on the MATLAB File menu, and then run it to see the following list displayed:

The input parameters for this program are
    'format'
    'maxHeight'
    'maxWidth'
    'outputDir'
    'script'

See Also

inputParser, addRequired(inputParser), addOptional(inputParser), parse(inputParser), createCopy(inputParser)

  


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