mustBeGreaterThan - Validate that value is greater than another value - MATLAB (original) (raw)
Validate that value is greater than another value
Syntax
Description
mustBeGreaterThan([value](#bvineip-A),[c](#bvineip-B))
throws an error if any elements in value
are less than or equal to the scalar c
. This function does not return a value.
mustBeGreaterThan
calls these functions to determine if value
is greater than c
:
Class support: All numeric classes, logical, and MATLABĀ® classes that overload the functions called by mustBeGreaterThan
.
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 mustBeGreaterThan
to validate that the values in the first input are greater than the value of the second input.
mustBeGreaterThan([2 3 4],2)
Value must be greater than 2.
This class restricts the value of Prop1
to be greater than 2
.
classdef MyClass properties Prop1 {mustBeGreaterThan(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 greater than 2.
When you assign a value to the property, MATLAB calls mustBeGreaterThan
with the value being assigned to the property. mustBeGreaterThan
issues an error because the value 2
is not greater than 2
.
This function restricts the input argument to be values that are greater than 5.
function r = mbGreaterThan(x) arguments x {mustBeGreaterThan(x,5)} end r = x - 5; end
Calling the function with a vector that contains values that are less than or equal to 5 does not meet the requirements defined with mustBeGreaterThan
and results in an error.
x = [12.7, 45.4, 5.0, 77.1, 53.1]; r = mbGreaterThan(x);
Error using mbGreaterThan (line 3) r = mbGreaterThan(x); ^ Invalid argument at position 1. Value must be greater than 5.
Tips
mustBeGreaterThan
is designed to be used for property and function argument validation.
Extended Capabilities
The mustBeGreaterThan
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 R2017a