mustBeNumericOrLogical - Validate that value is numeric or logical - MATLAB (original) (raw)
Main Content
Validate that value is numeric or logical
Syntax
Description
mustBeNumericOrLogical([value](#bvizqg6-A))
throws an error if value
is not numeric or logical. This function does not return a value.
mustBeNumericOrLogical
calls these functions to determine if the input is numeric or logical:
This function ignores input arguments that are empty values. Therefore, no error is thrown when the property or function argument value is empty.
Examples
Validate that the value of an object property is a numeric or logical value.
a = lettersPattern; mustBeNumericOrLogical(a)
Value must be numeric or logical.
In this case, the value of the a
property is apattern
object, which results in an error.
This class restricts the value of Prop1
to numeric values.
classdef MyClass properties Prop1 {mustBeNumericOrLogical} end end
Create an object and assign a value to its property.
obj = MyClass; obj.Prop1 = gca;
Error setting property 'Prop1' of class 'MyClass'. Value must be numeric or logical.
When you assign a value to the property, MATLABĀ® calls mustBeNumericOrLogical
with the value being assigned to the property. mustBeNumericOrLogical
issues an error because the value assigned to Prop1
is a char
vector.
This function restricts the input argument to a numeric or logical vector.
function r = mbNumericOrLogical(x) arguments x (1,:) {mustBeNumericOrLogical} end p = [3 2 1]; r = polyval(p,x); end
Calling this function with a character vector results in an error being thrown by mustBeNumericOrLogical
.
x = '4 3 2'; r = mbNumericOrLogical(x);
Error using mbNumericOrLogical (line 3) r = mbNumericOrLogical(x); ^ Invalid argument at position 1. Value must be numeric or logical.
Input Arguments
Value to validate, specified as a scalar or array of any one of the following:
Data Types: single
| double
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
| logical
Complex Number Support: Yes
Tips
mustBeNumericOrLogical
is designed to be used for property and function argument validation.
Extended Capabilities
The mustBeNumericOrLogical
function fully supports GPU arrays. To run the function on a GPU, specify the input data as a gpuArray (Parallel Computing Toolbox). For more information, see Run MATLAB Functions on a GPU (Parallel Computing Toolbox).
Version History
Introduced in R2017a