mustBeGreaterThanOrEqual
验证值大于等于另一个值
说明
示例
使用 mustBeGreaterThanOrEqual 验证数组中的值大于或等于 3。
mustBeGreaterThanOrEqual([2,3,4],3)
Value must be greater than or equal to 3.
将属性值限制为大于或等于指定的值。
该类将 Prop1 的值限制为大于等于 3。
classdef MyClass properties Prop1 {mustBeGreaterThanOrEqual(Prop1,3)} end end
创建一个对象,并向其属性赋值。
obj = MyClass; obj.Prop1 = 2;
Error setting property 'Prop1' of class 'MyClass'. Value must be greater than or equal to 3.
当您向属性赋值时,MATLAB 会使用赋给属性的值调用 mustBeGreaterThanOrEqual。mustBeGreaterThanOrEqual 将引发错误,因为值 2 不大于等于 3。
此函数将输入参量限制为大于或等于 5 的值。
function r = mbGreaterThanOrEqual(x) arguments x {mustBeGreaterThanOrEqual(x,5)} end r = x - 5; end
使用包含小于 5 的值的向量调用该函数不满足 mustBeGreaterThanOrEqual 定义的要求,并会引发错误。
x = [12.7, 45.4, 4.9, 77.1, 53.1]; r = mbGreaterThanOrEqual(x);
Error using mbGreaterThanOrEqual (line 3)
r = mbGreaterThanOrEqual(x);
^
Invalid argument at position 1. Value must be greater than or equal to 5.输入参数
要验证的值,指定为标量或由以下类型之一的值组成的数组:
logical、char、string或数值类实现
ge的 MATLAB 类
数据类型: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | logical | char | string | categorical | datetime | duration | table | timetable
复数支持: 是
常量值,value 参量必须大于或等于该值,指定为以下类型之一的标量或数组:
logical、char、string或数值类实现
ge的 MATLAB 类
输入 c 和 value 必须具有兼容的大小(例如,value 是 M×N 矩阵,c 是标量或 1×N 行向量)。有关详细信息,请参阅基本运算的兼容数组大小。
在 R2026a 之前的版本中: c 必须是标量。
数据类型: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | logical | char | string | categorical | datetime | duration | table | timetable
复数支持: 是
提示
mustBeGreaterThanOrEqual用于属性和函数参量验证。
扩展功能
C/C++ 代码生成
使用 MATLAB® Coder™ 生成 C 代码和 C++ 代码。
GPU 代码生成
使用 GPU Coder™ 为 NVIDIA® GPU 生成 CUDA® 代码。
mustBeGreaterThanOrEqual 函数完全支持 GPU 数组。要在 GPU 上运行该函数,请将输入数据指定为 gpuArray (Parallel Computing Toolbox)。有关详细信息,请参阅在 GPU 上运行 MATLAB 函数 (Parallel Computing Toolbox)。
mustBeGreaterThanOrEqual 函数完全支持分布式数组。有关详细信息,请参阅使用分布式数组运行 MATLAB 函数 (Parallel Computing Toolbox)。
版本历史记录
在 R2017a 中推出mustBeGreaterThanOrEqual 函数不再调用 isreal、isnumeric 和 islogical 函数来验证输入。因此,mustBeGreaterThanOrEqual 接受基础比较运算符 ge(或 >=)支持的任何数据类型,例如字符向量、字符串以及日期和时间类型。
例如,compareValues 函数使用 mustBeGreaterThanOrEqual 验证函数限制输入参量的值。
function obj = compareValues(startValue,endValue) arguments startValue endValue {mustBeGreaterThanOrEqual(endValue,startValue)} end end
compareValues 函数现在接受类型为 datetime 的数据作为输入。A = datetime("yesterday"); B = datetime("today"); compareValues(A,B)
在以前的版本中,使用 datetime 输入调用 compareValues 会返回以下错误:
Error using compareValues (line 4) Inputs to function 'mustBeGreaterThanOrEqual' must be numeric or logical.
要保留以前版本的行为,请在您的验证检查中显式使用 mustBeReal 和 mustBeNumericOrLogical。例如,以下代码保留 mustBeGreaterThanOrEqual 函数定义中 compareValues 的先前行为。
function obj = compareValues(startValue,endValue) arguments startValue {mustBeReal,mustBeNumericOrLogical} endValue {mustBeReal,mustBeNumericOrLogical, ... mustBeGreaterThanOrEqual(endValue,startValue)} end end
当比较具有兼容数组大小的两个输入时,mustBeGreaterThanOrEqual 函数现在支持隐式扩展,类似于算术运算中的隐式扩展行为。
例如,compareValues 函数使用 mustBeGreaterThanOrEqual 验证函数限制输入参量的值。
function obj = compareValues(startValue,endValue) arguments startValue endValue {mustBeGreaterThanOrEqual(endValue,startValue)} end end
compareValues 函数现在接受具有兼容数组大小的输入。compareValues([-2; -1],[3 2 1])
在以前的版本中,对 2×1 和 1×3 输入数组调用 compareValues 会返回以下错误:
Error using compareValues (line 4) Second input to function 'mustBeGreaterThanOrEqual' 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.
选择网站
选择网站以获取翻译的可用内容,以及查看当地活动和优惠。根据您的位置,我们建议您选择:。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- 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)