Next: , Previous: , Up: Validating Arguments   [Contents][Index]


11.9.2 Validating the type of Arguments

Besides the number of arguments, inputs can be checked for various properties. validatestring is used for string arguments and validateattributes for numeric arguments.

: validstr = validatestring (str, strarray)
: validstr = validatestring (str, strarray, funcname)
: validstr = validatestring (str, strarray, funcname, varname)
: validstr = validatestring (…, position)

Verify that str is an element, or substring of an element, in strarray.

When str is a character string to be tested, and strarray is a cellstr of valid values, then validstr will be the validated form of str where validation is defined as str being a member or substring of validstr. This is useful for both verifying and expanding short options, such as "r", to their longer forms, such as "red". If str is a substring of validstr, and there are multiple matches, the shortest match will be returned if all matches are substrings of each other. Otherwise, an error will be raised because the expansion of str is ambiguous. All comparisons are case insensitive.

The additional inputs funcname, varname, and position are optional and will make any generated validation error message more specific.

Examples:

validatestring ("r", {"red", "green", "blue"})
⇒ "red"

validatestring ("b", {"red", "green", "blue", "black"})
⇒ error: validatestring: multiple unique matches were found for 'b':
   blue, black

See also: strcmp, strcmpi, validateattributes, inputParser.

: validateattributes (A, classes, attributes)
: validateattributes (A, classes, attributes, arg_idx)
: validateattributes (A, classes, attributes, func_name)
: validateattributes (A, classes, attributes, func_name, arg_name)
: validateattributes (A, classes, attributes, func_name, arg_name, arg_idx)

Check validity of input argument.

Confirms that the argument A is valid by belonging to one of classes, and holding all of the attributes. If it does not, an error is thrown, with a message formatted accordingly. The error message can be made further complete by the function name fun_name, the argument name arg_name, and its position in the input arg_idx.

classes must be a cell array of strings (an empty cell array is allowed) with the name of classes (remember that a class name is case sensitive). In addition to the class name, the following categories names are also valid:

"float"

Floating point value comprising classes "double" and "single".

"integer"

Integer value comprising classes (u)int8, (u)int16, (u)int32, (u)int64.

"numeric"

Numeric value comprising either a floating point or integer value.

attributes must be a cell array with names of checks for A. Some of them require an additional value to be supplied right after the name (see details for each below).

"<="

All values are less than or equal to the following value in attributes.

"<"

All values are less than the following value in attributes.

">="

All values are greater than or equal to the following value in attributes.

">"

All values are greater than the following value in attributes.

"2d"

A 2-dimensional matrix. Note that vectors and empty matrices have 2 dimensions, one of them being of length 1, or both length 0.

"3d"

Has no more than 3 dimensions. A 2-dimensional matrix is a 3-D matrix whose 3rd dimension is of length 1.

"binary"

All values are either 1 or 0.

"column"

Values are arranged in a single column.

"decreasing"

No value is NaN, and each is less than the preceding one.

"diag"

Value is a diagonal matrix.

"even"

All values are even numbers.

"finite"

All values are finite.

"increasing"

No value is NaN, and each is greater than the preceding one.

"integer"

All values are integer. This is different than using isinteger which only checks its an integer type. This checks that each value in A is an integer value, i.e., it has no decimal part.

"ncols"

Has exactly as many columns as the next value in attributes.

"ndims"

Has exactly as many dimensions as the next value in attributes.

"nondecreasing"

No value is NaN, and each is greater than or equal to the preceding one.

"nonempty"

It is not empty.

"nonincreasing"

No value is NaN, and each is less than or equal to the preceding one.

"nonnan"

No value is a NaN.

"nonnegative"

All values are non negative.

"nonsparse"

It is not a sparse matrix.

"nonzero"

No value is zero.

"nrows"

Has exactly as many rows as the next value in attributes.

"numel"

Has exactly as many elements as the next value in attributes.

"odd"

All values are odd numbers.

"positive"

All values are positive.

"real"

It is a non-complex matrix.

"row"

Values are arranged in a single row.

"scalar"

It is a scalar.

"size"

Its size has length equal to the values of the next in attributes. The next value must is an array with the length for each dimension. To ignore the check for a certain dimension, the value of NaN can be used.

"square"

Is a square matrix.

"vector"

Values are arranged in a single vector (column or vector).

See also: isa, validatestring, inputParser.

As alternative to validateattributes there are several shorter convenience functions to check for individual properties.

: mustBeFinite (x)

Requires that input x is finite.

Raises an error if any element of the input x is not finite, as determined by isfinite (x).

: mustBeGreaterThan (x, c)

Requires that input x is greater than c.

Raises an error if any element of the input x is not greater than c, as determined by x > c.

: mustBeGreaterThanOrEqual (x, c)

Requires that input x is greater than or equal to c.

Raises an error if any element of the input x is not greater than or equal to c, as determined by x >= c.

: mustBeInteger (x)

Requires that input x is integer-valued (but not necessarily integer-typed).

Raises an error if any element of the input x is not a finite, real, integer-valued numeric value, as determined by various checks.

: mustBeLessThan (x, c)

Requires that input x is less than c.

Raises an error if any element of the input x is not less than c, as determined by x < c.

: mustBeLessThanOrEqual (x, c)

Requires that input is less than or equal to a given value.

Raises an error if any element of the input x is not less than or equal to c, as determined by x <= c.

: mustBeMember (x, valid)

Requires that input x is a member of a set of given valid values.

Raises an error if any element of the input x is not a member of valid, as determined by ismember (x).

Note that char inputs may behave weirdly, because of the interaction between chars and cellstrings when calling ismember() on them. But it will probably "do what you mean" if you just use it naturally.

: mustBeNegative (x)

Requires that input x is negative.

Raises an error if any element of the input x is not negative, as determined by x < 0.

: mustBeNonempty (x)

Requires that input x is nonempty.

Raises an error if the input x is not empty, as determined by ! isempty (x).

: mustBeNonNan (x)

Requires that input x is non-NaN.

Raises an error if any element of the input x is NaN, as determined by isnan (x).

: mustBeNonnegative (x)

Requires that input x is not negative.

Raises an error if any element of the input x is negative, as determined by x >= 0.

: mustBeNonpositive (x)

Requires that input x is not positive.

Raises an error if any element of the input x is positive, as determined by x <= 0.

: mustBeNonsparse (x)

Requires that input x is not sparse.

Raises an error if the input x is sparse, as determined by issparse (x).

: mustBeNonzero (x)

Requires that input x is not zero.

Raises an error if any element of the input x is zero, as determined by x != 0.

: mustBeNumeric (x)

Requires that input x is numeric.

Raises an error if the input x is not numeric, as determined by isnumeric (x).

: mustBeNumericOrLogical (x)

Requires that input x is numeric or logical.

Raises an error if the input x is not numeric or logical, as determined by isnumeric (x) || islogical (x).

: mustBePositive (x)

Requires that input x is positive.

Raises an error if any element of the input x is not positive, as determined by x > 0.

: mustBeReal (x)

Requires that input x is real.

Raises an error if the input x is not real, as determined by isreal (x).


Next: , Previous: , Up: Validating Arguments   [Contents][Index]