帮助中心帮助中心
本页的翻译已过时。点击此处可查看最新英文版本。
验证值为整数
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)
Error using mustBeInteger (line 14) Values must be integer.
如果两个随机整数相除的结果带有小数部分,mustBeInteger 会引发错误。
此类将属性的值限制为仅允许整数。
classdef MyClass properties Prop1 {mustBeInteger} end end
创建一个对象,并向其属性赋值。将两个随机整数相除,得到的可以是整数,也可以是带有小数部分的数。
obj = MyClass; obj.Prop1 = randi(9)/randi(9);
Error setting 'Prop1' property of 'MyClass' class: 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 r = mbInteger(A,idx); ↑ Invalid input argument at position 2. Value must be integer.
要验证的值,指定为标量或由以下任一类型的值组成的数组:
logical、char 或数值类
char
实现 eq、isreal、isfinite、floor 以及 isnumeric 或 islogical 的 MATLAB 类
其他数据类型会导致错误。
mustBeInteger 用于属性和函数参数验证。
mustBeNumeric | mustBePositive | arguments
mustBeNumeric
mustBePositive
arguments
您点击的链接对应于以下 MATLAB 命令:
请在 MATLAB 命令行窗口中直接输入以执行命令。Web 浏览器不支持 MATLAB 命令。
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Contact your local office