Main Content

mustBeGreaterThanOrEqual

Validate that value is greater than or equal to another value

Description

mustBeGreaterThanOrEqual(value,c) throws an error if any elements in value are less than the scalar c. This function does not return a value.

mustBeGreaterThanOrEqual calls these functions to determine if value is greater than or equal to c:

Class support: All numeric classes, logical, and MATLAB® classes that overload the functions called by mustBeGreaterThanOrEqual.

This function ignores empty values in the first input argument. Therefore, no error is thrown when the property or function argument value is empty.

example

Examples

collapse all

Use mustBeGreaterThanOrEqual to validate that the values in the array are greater than or equal to 3.

mustBeGreaterThanOrEqual([2,3,4],3)
Value must be greater than or equal to 3.

Restrict property values to be greater than or equal to a specified value.

This class constrains the value of Prop1 to be greater than or equal to 3.

classdef MyClass
   properties
      Prop1 {mustBeGreaterThanOrEqual(Prop1,3)}
   end
end

Create an object and assign a value to its property.

obj = MyClass;
obj.Prop1 = 2;
Error setting property 'Prop1' of class 'MyClass'. Value must be greater than or equal to 3.

When you assign a value to the property, MATLAB calls mustBeGreaterThanOrEqual with the value being assigned to the property. mustBeGreaterThanOrEqual issues an error because the value 2 is not greater than or equal to 3.

This function restricts the input argument to be values that are greater than or equal to 5.

function r = mbGreaterThanOrEqual(x)
    arguments
        x {mustBeGreaterThanOrEqual(x,5)}
    end
    r = x - 5;
end

Calling the function with a vector that contains values that are less than 5 does not meet the requirements defined with mustBeGreaterThanOrEqual and results in an error.

x = [12.7, 45.4, 4.9, 77.1, 53.1];
r = mbGreaterThanOrEqual(x);
Error using mbGreaterThanOrEqual (line 3)
 r = mbGreaterThanOrEqual(x);
                          ^
Invalid argument at position 1. Value must be greater than or equal to 5.

Input Arguments

collapse all

Value to validate, specified as a scalar or an array of one of the following:

Constant value that the value argument must be greater than or equal to, specified as a scalar of one of the following:

Tips

  • mustBeGreaterThanOrEqual 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