Main Content

mustBeNumericOrLogical

Validate that value is numeric or logical

Description

example

mustBeNumericOrLogical(value) 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

collapse all

Validate that the value of an object property is a numeric or logical value.

a = lettersPattern;
mustBeNumericOrLogical(a)
Values must be numeric or logical.

In this case, the value of the a property is a pattern 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 = get(gca,'Visible');
Error setting 'Prop1' property of 'MyClass' class:
Values 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
 r = mbNumericOrLogical(x);
                       ↑
Invalid input argument at position 1. Value must be numeric or logical.

Input Arguments

collapse all

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

C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.

Version History

Introduced in R2017a