mustBeLessThanOrEqual
Validate that value is less than or equal to another value
Description
mustBeLessThanOrEqual(
throws an error if any elements in value,c)value are greater than
c. This function does not return a value.
mustBeLessThanOrEqual
calls the le function to determine
if value is less than or equal to c. (since R2026a)
Class support: All numeric classes, logical, and MATLAB® classes that implement le.
This function ignores empty values in the first input argument. Therefore, no error is thrown when the property or function argument value is empty.
Examples
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
Value to validate, specified as a scalar or an array of one of these types:
logical,char,string, or numeric classMATLAB classes that implement
le
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 or
equal to, specified as a scalar or an array of one of these types:
logical,char,string, or numeric classMATLAB classes that implement
le
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
mustBeLessThanOrEqualis designed to be used for property and function argument validation.
Extended Capabilities
C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.
GPU Code Generation
Generate CUDA® code for NVIDIA® GPUs using GPU Coder™.
The mustBeLessThanOrEqual 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).
The mustBeLessThanOrEqual function fully supports
distributed arrays. For more information, see Run MATLAB Functions with Distributed Arrays (Parallel Computing Toolbox).
Version History
Introduced in R2017aThe mustBeLessThanOrEqual function no longer calls the isreal, isnumeric, and islogical functions to validate input. As a result,
mustBeLessThanOrEqual accepts any data type supported by the
underlying comparison operator le (or
<=), such as character vectors, strings, and date and time
types.
For example, the compareValues function restricts input argument
values using the mustBeLessThanOrEqual validation
function.
function obj = compareValues(startValue,endValue) arguments startValue endValue {mustBeLessThanOrEqual(startValue,endValue)} end end
compareValues function now accepts data of
type datetime as
input.A = datetime("yesterday"); B = datetime("today"); compareValues(A,B)
In previous releases, calling compareValues with
datetime inputs returned this
error:
Error using compareValues (line 4) Inputs to function 'mustBeLessThanOrEqual' must be numeric or logical.
To preserve the behavior of previous releases, explicitly use mustBeReal and mustBeNumericOrLogical in your validation
checks. For example, this code preserves the previous behavior of
mustBeLessThanOrEqual in the compareValues
function
definition.
function obj = compareValues(startValue,endValue) arguments startValue {mustBeReal,mustBeNumericOrLogical} endValue {mustBeReal,mustBeNumericOrLogical, ... mustBeLessThanOrEqual(startValue,endValue)} end end
The mustBeLessThanOrEqual function now supports implicit expansion
when comparing two inputs with compatible array sizes, similar to the
implicit expansion behavior in arithmetic operations.
For example, the compareValues function restricts input argument
values using the mustBeLessThanOrEqual validation
function.
function obj = compareValues(startValue,endValue) arguments startValue endValue {mustBeLessThanOrEqual(startValue,endValue)} end end
compareValues function now accepts inputs
with compatible array
sizes.compareValues([-2; -1],[3 2 1])
In previous releases, calling compareValues on 2-by-1 and 1-by-3
input arrays returned this
error:
Error using compareValues (line 4) Second input to function 'mustBeLessThanOrEqual' must be a scalar.
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Seleccione un país/idioma
Seleccione un país/idioma para obtener contenido traducido, si está disponible, y ver eventos y ofertas de productos y servicios locales. Según su ubicación geográfica, recomendamos que seleccione: .
También puede seleccionar uno de estos países/idiomas:
Cómo obtener el mejor rendimiento
Seleccione China (en idioma chino o inglés) para obtener el mejor rendimiento. Los sitios web de otros países no están optimizados para ser accedidos desde su ubicación geográfica.
América
- América Latina (Español)
- Canada (English)
- United States (English)
Europa
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)