parse
Parse function inputs
Syntax
Description
Examples
Validate Required Input Is Nonnegative
Create an input parser scheme that checks that a required input is a
nonnegative, numeric scalar. The syntax @(x)
creates a handle to an
anonymous function with one input.
p = inputParser;
argName = 'num';
validationFcn = @(x) (x > 0) && isnumeric(x) && isscalar(x);
addRequired(p,argName,validationFcn)
Parse an invalid input, such as -1
:
parse(p,-1)
The value of 'num' is invalid. It must satisfy the function: @(x)(x>0)&&isnumeric(x)&&isscalar(x).
Input Parsing
Parse and validate required and optional function inputs.
Create a function in the file findArea.m
. The
findArea
function requires the width
input
argument and accepts a variable number of additional inputs. The input parser scheme
specifies these argument conditions:
width
(required argument). Since required arguments are positional,width
must be the first argument to thefindArea
function. The input parser checks thatwidth
is positive, scalar, and numeric.height
(optional argument). Since optional arguments are positional, ifheight
is an argument to thefindArea
function, then it must be the second argument. The input parser checks thatheight
is positive, scalar, and numeric.'units'
and its associated value (name-value pair). Name-value pairs are optional. When you call thefindArea
function, specify name-value pairs in any order after positional arguments. The input parser checks that the value for'units'
is a string.'shape'
and its associated value (another name-value pair). The input parser checks that the value for'shape'
is contained in theexpectedShapes
array.
function a = findArea(width,varargin) defaultHeight = 1; defaultUnits = 'inches'; defaultShape = 'rectangle'; expectedShapes = {'square','rectangle','parallelogram'}; p = inputParser; validScalarPosNum = @(x) isnumeric(x) && isscalar(x) && (x > 0); addRequired(p,'width',validScalarPosNum); addOptional(p,'height',defaultHeight,validScalarPosNum); addParameter(p,'units',defaultUnits,@isstring); addParameter(p,'shape',defaultShape,... @(x) any(validatestring(x,expectedShapes))); parse(p,width,varargin{:}); a = p.Results.width*p.Results.height; end
Call the findArea
function several times. The input parser does not
throw an error for any of these function calls.
a = findArea(7); a = findArea(7,3); a = findArea(13,'shape','square'); a = findArea(13,'units',"miles",'shape','square');
Call the function with arguments that do not match the input parser scheme. Specify a
nonnumeric value for the width
input:
a = findArea('text')
Error using findArea (line 14) The value of 'width' is invalid. It must satisfy the function: @(x)isnumeric(x)&&isscalar(x)&&(x>0).
Specify an unsupported value for 'shape'
.
a = findArea(4,12,'shape','circle')
Error using findArea (line 14) The value of 'shape' is invalid. Expected input to match one of these values: 'square', 'rectangle', 'parallelogram' The input, 'circle', did not match any of the valid values.
Input Arguments
p
— Input parser scheme
inputParser
object
Input parser scheme, specified as an inputParser
object.
argList
— Inputs to parse and validate
comma-separated list
Inputs to parse and validate, specified as a comma-separated list. The
elements of argList
can be any data type. The input
parser determines argument validity using the validation function you
specified when you added arguments to the input parser scheme.
Example: 'textA',13,mtxB
Example: varargin{:}
Extended Capabilities
Thread-Based Environment
Run code in the background using MATLAB® backgroundPool
or accelerate code with Parallel Computing Toolbox™ ThreadPool
.
Version History
Introduced in R2007a
See Also
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)