Main Content

mustBeLessThanOrEqual

Validate that value is less than or equal to another value

Description

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

mustBeLessThanOrEqual calls these functions to determine if value is less than or equal to c:

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

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 mustBeLessThanOrEqual to validate that the values in the first input are less than or equal to the value of the second input.

mustBeLessThanOrEqual([3 4 5],2)
Value must be less than or equal to 2.

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

This class constrains the value of Prop1 to be less than or equal to 2.

classdef MyClass
   properties
      Prop1 {mustBeLessThanOrEqual(Prop1,2)}
   end
end

Create an object and assign a value to its property.

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

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

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

function r = mbLessThanOrEqual(x)
    arguments
        x {mustBeLessThanOrEqual(x,5)}
    end
    r = x + 5;
end

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

x = [1.27, 4.54, 3.9, 5.1, .531];
r = mbLessThanOrEqual(x);
Error using mbLessThanOrEqual (line 3)
 r = mbLessThanOrEqual(x);
                       ^
Invalid argument at position 1. Value must be less 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 less than or equal to, specified as a scalar of one of the following:

Tips

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