mustBeBetween - Validate that all elements are within specified range - MATLAB (original) (raw)
Validate that all elements are within specified range
Since R2025a
Syntax
Description
mustBeBetween([A](#mw%5F8a5709e6-b646-4c09-8e61-4ad04edc3b16),[lower](#mw%5Fd6b9ed1c-8c77-4314-9c13-a8d617cf8052),[upper](#mw%5F8d6d87a2-045c-4a64-8775-1aecfdb774f9))
throws an error if any element in A
is outside the range defined bylower
and upper
. A value is within the range if it is greater than or equal to lower
, and less than or equal toupper
. This function does not return a value.
mustBeBetween
calls the allbetween function to determine if all elements in A
are within the specified range.
Class support: All numeric classes, logical
, char
,string
, categorical
, datetime
,duration
, and MATLAB® classes that overload allbetween
orisbetween
.
mustBeBetween([A](#mw%5F8a5709e6-b646-4c09-8e61-4ad04edc3b16),[lower](#mw%5Fd6b9ed1c-8c77-4314-9c13-a8d617cf8052),[upper](#mw%5F8d6d87a2-045c-4a64-8775-1aecfdb774f9),[intervalType](#mw%5F398fe0e6-575a-41c5-a917-4ba85a15f439))
specifies the type of interval. For example,mustBeBetween(A,lower,upper,"open")
validates that all elements inA
are within the open interval (lower, upper)
.
mustBeBetween(___,DataVariables=[vars](#mw%5F472d1820-e6f0-4702-9c8a-9092352508a6))
specifies the table or timetable variables to operate on in addition to any of the input argument combinations in previous syntaxes. For example, for table A
,mustBeBetween(A,lower,upper,DataVariables="Var1")
validates that elements in table variable Var1
are within the specified range.
Examples
Validate that all elements in a vector are within the open interval (0, 5). mustBeBetween
throws an error because 5
is equal to the upper bound, which is excluded from the range.
mustBeBetween([3 4 5],0,5,"open")
Value must be within the range specified by 0 and 5.
Specify the range as the closed interval [0, 5]. mustBeBetween
does not throw an error.
mustBeBetween([3 4 5],0,5,"closed")
You can restrict the range of values that a function accepts as input by using the mustBeBetween
function in its arguments block. For example, the mustBeBetween
function definition restricts the input argument to values within the range [0, 5].
function b = checkBetween(x) arguments x {mustBeBetween(x,0,5)} end b = x - 5; end
Call the checkBetween
function with a value outside the allowed range. MATLAB calls mustBeBetween
and throws an error becausex
is not within the allowed range.
x = 10; b = checkBetween(x);
Error using checkBetween (line 3) b = checkBetween(x); ^ Invalid argument at position 1. Value must be within the range specified by 0 and 5.
If you call the checkBetween
function with a value within the allowed range, mustBeBetween
does not throw an error.
x = 3; b = checkBetween(x);
Input Arguments
Values to validate, specified as an array of one of these types or as a table or timetable containing variables of these types:
- All numeric classes,
logical
,char
,string
,categorical
,datetime
, orduration
- All MATLAB classes that overload
allbetween
orisbetween
Data Types: single
| double
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
| logical
| char
| string
| categorical
| datetime
| duration
| table
| timetable
Lower bound of the range, specified as an array or one-row table. The lower and upper bounds must either be the same size or have sizes that are compatible. The bounds must also have the same data type asA
or a type that can be compared with A
.
- To use the same lower bound for all elements of
A
, specifylower
as a scalar. - To use different lower bounds for each column or row in
A
, specifylower
as a row or column vector, respectively. - To use a different lower bound for each data element, specify
lower
as an array of the same size asA
.
If the values to validate are in a table or timetable, when the table variables to operate on have different data types, specify the lower bound as a one-row table. The variable names of the one-row table must be the same as the names of the table variables to operate on.
Upper bound of the range, specified as an array or one-row table. The lower and upper bounds must either be the same size or have sizes that are compatible. The bounds must also have the same data type asA
or a type that can be compared with A
.
- To use the same upper bound for all elements of
A
, specifyupper
as a scalar. - To use different upper bounds for each column or row in
A
, specifyupper
as a row or column vector, respectively. - To use a different upper bound for each data element, specify
upper
as an array of the same size asA
.
If the values to validate are in a table or timetable, when the table variables to operate on have different data types, specify the upper bound as a one-row table. The variable names of the one-row table must be the same as the names of the table variables to operate on.
Type of interval that defines the range of allowed values, specified as one of the values in this table.
Type of Interval | Diagram | Description |
---|---|---|
"closed" | ![]() |
Include lower andupper. |
"open" | ![]() |
Exclude lower andupper. |
"openleft" or"closedright" | ![Half-open interval (lower, upper]](https://in.mathworks.com/help/matlab/ref/interval-notation-04.png) | Exclude lower and includeupper."openleft" and"closedright" have the same behavior. |
"openright" or"closedleft" | ![Half-open interval lower, upper) | Include lower and excludeupper."openright" and"closedleft" have the same behavior. |
Table or timetable variables to operate on, specified as one of the values in this table. mustBeBetween
operates only on elements in the specified variables in A
.
If you do not specify vars
, mustBeBetween
operates on all variables in A
.
Indexing Scheme | Values to Specify | Examples |
---|---|---|
Variable name | A string scalar or character vectorA string array or cell array of character vectorsA pattern object | "A" or 'A' — A variable named A["A" "B"] or {'A','B'} — Two variables named A andB"Var"+digitsPattern(1) — Variables named"Var" followed by a single digit |
Variable index | An index number that refers to the location of a variable in the tableA vector of numbersA logical vector. Typically, this vector is the same length as the number of variables, but you can omit trailing0 (false) values. | 3 — The third variable from the table[2 3] — The second and third variables from the table[false false true] — The third variable |
Function handle | A function handle that takes a table variable as input and returns a logical scalar | @isnumeric — All the variables containing numeric values |
Variable type | A vartype subscript that selects variables of a specified type | vartype("numeric") — All the variables containing numeric values |
Example: mustBeBetween(A,lower,upper,DataVariables=["Var1" "Var2" "Var4"])
validates that the elements in the Var1
,Var2
, and Var4
variables in tableA
are within the specified range.
Tips
mustBeBetween
is designed to be used for property and function argument validation.
Extended Capabilities
The mustBeBetween
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).
Version History
Introduced in R2025a