MATLAB 帮助中心
验证值为整数
mustBeInteger(value)
如果 value 不包含整数值,mustBeInteger(value) 会抛出错误。此函数测试数字是否带有小数部分。此处不考虑值的数据类型。mustBeInteger 不返回值。
value
mustBeInteger
mustBeInteger 调用以下函数来确定 value 是否为整数:
isreal
isfinite
floor
eq
isnumeric
islogical
支持的类:重载 mustBeInteger 调用的函数的所有数值类、logical 和 MATLAB® 类。
logical
此函数忽略输入参量中的空值。因此,当属性或函数参量值为空时,不会抛出错误。
示例
全部折叠
验证计算结果是整数值。
A = randi(9)/randi(9); mustBeInteger(A)
Value must be integer.
如果两个随机整数相除的结果带有小数部分,mustBeInteger 会引发错误。
此类将属性的值限制为仅允许整数。
classdef MyClass properties Prop1 {mustBeInteger} end end
创建一个对象,并向其属性赋值。将两个随机整数相除,得到的可以是整数,也可以是带有小数部分的数。
obj = MyClass; obj.Prop1 = randi(9)/randi(9);
Error setting property 'Prop1' of class 'MyClass'. Value must be integer.
当您向属性赋值时,MATLAB 会使用赋给属性的值调用 mustBeInteger。当这些特定随机整数的除法运算的结果值不是整数时,mustBeInteger 将引发错误。
此函数将输入参量限制为不带小数部分的值。
function r = mbInteger(A,idx) arguments A (1,:) {mustBeNumeric} idx (1,1) {mustBeInteger} end r = A(idx).^2; end
使用非整数值调用该函数会导致 mustBeInteger 抛出错误。将两个随机整数相除,得到的可以是整数,也可以是带有小数部分的数。
idx = randi(9)/randi(9); A = rand(1,10); r = mbInteger(A,idx);
Error using mbInteger (line 4) r = mbInteger(A,idx); ^^^ Invalid argument at position 2. Value must be integer.
要验证的值,指定为标量或由以下任一类型的值组成的数组:
logical、char 或数值类
char
实现 eq、isreal、isfinite、floor 以及 isnumeric 或 islogical 的 MATLAB 类
其他数据类型会导致错误。
mustBeInteger 用于属性和函数参量验证。
全部展开
此函数完全支持分布式数组。有关详细信息,请参阅使用分布式数组运行 MATLAB 函数 (Parallel Computing Toolbox)。
在 R2017a 中推出
arguments | mustBeNumericOrLogical | mustBeReal | mustBeNumeric | mustBePositive
arguments
mustBeNumericOrLogical
mustBeReal
mustBeNumeric
mustBePositive
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 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
欧洲
亚太
联系您当地的办事处