Main Content

mustBeLessThan

Validate that value is less than another value

Description

mustBeLessThan(value,c) throws an error if any elements in value are greater than or equal to c. This function does not return a value.

mustBeLessThan calls the lt function to determine if value is less than c. (since R2026a)

Class support: All numeric classes, logical, and MATLAB® classes that implement lt.

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

mustBeLessThan([2 3 4],2)
Value must be less than 2.

Restrict property values to be less than a specified value.

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

classdef MyClass
   properties
      Prop1 {mustBeLessThan(Prop1,2)}
   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 less than 2.

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

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

function r = mbLessThan(x)
    arguments
        x {mustBeLessThan(x,5)}
    end
    r = x + 5;
end

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

x = [1.27, 4.54, 3.9, 5.0, .531];
r = mbLessThan(x);
Error using mbLessThan (line 3)
 r = mbLessThan(x);
                ^
Invalid argument at position 1. Value must be less than 5.

Input Arguments

collapse all

Value to validate, specified as a scalar or an array of one of these types:

  • logical, char, string, or numeric class

  • MATLAB classes that implement lt

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | logical | char | string | categorical | datetime | duration | table | timetable
Complex Number Support: Yes

Constant value that the value argument must be less than, specified as a scalar or an array of one of these types:

  • logical, char, string, or numeric class

  • MATLAB classes that implement lt

Inputs c and value must have compatible sizes (for example, value is an M-by-N matrix and c is a scalar or 1-by-N row vector). For more information, see Compatible Array Sizes for Basic Operations.

Before R2026a: c must be scalar.

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | logical | char | string | categorical | datetime | duration | table | timetable
Complex Number Support: Yes

Tips

  • mustBeLessThan is designed to be used for property and function argument validation.

Extended Capabilities

expand all

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

GPU Code Generation
Generate CUDA® code for NVIDIA® GPUs using GPU Coder™.

Version History

Introduced in R2017a

expand all