validateattributes
检查数组的有效性
语法
说明
validateattributes(
验证数组 A
,classes
,attributes
)A
是否属于至少一个指定的类(或其子类)并具有所有指定的属性。如果 A
不符合条件,MATLAB® 将引发错误,并显示一条格式化的错误消息。否则,validateattributes
将完成并且不显示任何输出。
validateattributes(
会将输入在函数参数列表中的位置包括在视情境生成的错误消息中,作为消息的一部分进行输出。A
,classes
,attributes
,argIndex
)
validateattributes(
将指定的函数名称包括在生成的错误标识符中。A
,classes
,attributes
,funcName
)
示例
验证数组大小
classes = {'numeric'}; attributes = {'size',[4,6,2]}; A = rand(3,5,2); validateattributes(A,classes,attributes)
Expected input to be of size 4x6x2 when it is actually size 3x5x2.
因为 A
与指定的属性不匹配,所以 MATLAB 将引发一条错误消息。
验证数组单调性
确定数组是递增还是非递减。
A = [1 5 8 2; 9 6 9 4] validateattributes(A, {'double'},{'nondecreasing'}) validateattributes(A, {'double'},{'increasing'})
A = 1 5 8 2 9 6 9 4
由于 A
同时具有递增和非递减的特点,因此 validateattributes
在进行这两项属性检查时均不会引发错误。
将 A(2,3)
设置为等于 A(1,3)
会导致产生不再严格递增的列,因此 validateattributes
会引发错误。
A(2,3) = 8 validateattributes(A, {'double'},{'increasing'})
A = 1 5 8 2 9 6 8 4 Expected input to be strictly increasing.
但是,由于各列元素均大于等于其上一个列元素,因此,这些列仍然为非递减。下列代码不会引发错误。
validateattributes(A, {'double'},{'nondecreasing'})
检查复数属性
假定 a
是某个函数的第二个输入参数,检查它是否为非负数。
a = complex(1,1); validateattributes(a,{'numeric'},{'nonnegative'},2)
Expected input number 2 to be nonnegative.
因为复数缺少在复平面中明确定义的排序,validateattributes
无法将复数识别为正数或负数。
确保数组值在指定的范围内
检查数组中的值是否为从 0 到 10 的 8 位整数。
假定此代码位于调用 Rankings
的函数中。
classes = {'uint8','int8'}; attributes = {'>',0,'<',10}; funcName = 'Rankings'; A = int8(magic(4)); validateattributes(A,classes,attributes,funcName)
Error using Rankings Expected input to be an array with all of the values < 10.
使用 inputParser
验证函数输入参数
创建一个通过 inputParser
检查输入参数的自定义函数,并使用 validateattributes
作为 addRequired
和 addOptional
方法的验证函数。
定义该函数。
function a = findArea(shape,dim1,varargin) p = inputParser; charchk = {'char'}; numchk = {'numeric'}; nempty = {'nonempty'}; addRequired(p,'shape',@(x)validateattributes(x,charchk,nempty)) addRequired(p,'dim1',@(x)validateattributes(x,numchk,nempty)) addOptional(p,'dim2',1,@(x)validateattributes(x,numchk,nempty)) parse(p,shape,dim1,varargin{:}) switch shape case 'circle' a = pi * dim1.^2; case 'rectangle' a = dim1 .* p.Results.dim2; end end
调用该函数并且第三个输入为非数值。
myarea = findArea('rectangle',3,'x')
Error using findArea (line 10) The value of 'dim2' is invalid. Expected input to be one of these types: double, single, uint8, uint16, uint32, uint64, int8, int16, int32, int64
验证函数参数
检查函数的输入,并在生成的错误中包括有关输入名称和位置的信息。
定义该函数。
function v = findVolume(shape,ht,wd,ln) validateattributes(shape,{'char'},{'nonempty'},mfilename,'Shape',1) validateattributes(ht,{'numeric'},{'nonempty'},mfilename,'Height',2) validateattributes(wd,{'numeric'},{'nonempty'},mfilename,'Width',3) validateattributes(ln,{'numeric'},{'nonempty'},mfilename,'Length',4)
调用该函数而不带 shape
输入参数。
vol = findVolume(10,7,4)
Error using findVolume Expected input number 1, Shape, to be one of these types: char Instead its type was double. Error in findVolume (line 2) validateattributes(shape,{'char'},{'nonempty'},mfilename,'Shape',1)
函数名称作为错误标识符的一部分。
MException.last.identifier
ans = MATLAB:findVolume:invalidType
输入参数
A
— 输入
任何类型的数组
输入,指定为任何类型的数组。
数据类型: single
| double
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
| logical
| char
| string
| struct
| cell
| function_handle
复数支持: 是
classes
— 有效数据类型
字符向量 | 字符向量元胞数组 | 字符串数组
有效属性类型,指定为字符向量、字符向量元胞数组或字符串数组。classes
的每个元素都可以是任何内置或自定义类的名称,包括:
'half' | 半精度数 |
'single' | 单精度数 |
'double' | 双精度数 |
'int8' | 有符号 8 位整数 |
'int16' | 有符号 16 位整数 |
'int32' | 有符号 32 位整数 |
'int64' | 有符号 64 位整数 |
'uint8' | 无符号 8 位整数 |
'uint16' | 无符号 16 位整数 |
'uint32' | 无符号 32 位整数 |
'uint64' | 无符号 64 位整数 |
'logical' | 逻辑值 1 (true ) 或 0 (false ) |
'char' | 字符 |
'string' | 字符串数组 |
'struct' | 结构体数组 |
'cell' | 元胞数组 |
'table' | 表 |
'timetable' | 时间表 |
'function_handle' | 函数句柄 |
'numeric' | isa(A,'numeric') 函数为其返回 true 的任何数据类型,包括 int8 、int16 、int32 、int64 、uint8 、uint16 、uint32 、uint64 、single 或 double |
'< | 任何其他类名 |
数据类型: cell
| string
attributes
— 有效属性
元胞数组 | 字符串数组
有效属性,指定为元胞数组或字符串数组。
某些属性还需要数值,例如,用于指定 A
的元素的大小或数量的属性。对于这些属性,数值或向量必须紧跟在元胞数组中的属性名称的后面。字符串数组不能用于表示 attributes
中的数值。
这些属性描述数组 A
的大小和形状。
'2d' | 二维数组,包括标量、向量、矩阵和空数组 |
'3d' | 维度不大于三的数组 |
'column' | 列向量,N ×1 |
'row' | 行向量,1×N |
'scalar' | 标量值,1×1 |
'scalartext' | 字符串标量或字符向量,包括无任何字符的输入 |
'vector' | 行或列向量,或标量值 |
'size', [d1,...,dN] | 维度为 d1 -×-...-×-dN 的数组。要跳过检查特定维度的步骤,请为该维度指定 NaN ,例如 [3,4,NaN,2] 。 |
'numel', N | 具有 N 个元素的数组 |
'ncols', N | 具有 N 列的数组 |
'nrows', N | 具有 N 行的数组 |
'ndims', N | N 维数组 |
'square' | 方阵;换句话说,行数和列数相等的二维数组 |
'diag' | 对角矩阵 |
'nonempty' | 不存在为零的维度 |
'nonsparse' | 非稀疏数组 |
这些属性为 A
中的值指定有效范围。
'>', N | 大于 N 的所有值 |
'>=', N | 大于或等于 N 的所有值 |
'<', N | 小于 N 的所有值 |
'<=', N | 小于或等于 N 的所有值 |
'finite' | 所有值均为有限值 |
'nonnan' | 没有 NaN 值(非数字) |
这些属性检查数值数组或逻辑数组 A
中值的类型。
'binary' | 由一和零组成的数组 |
'even' | 由偶数(包括零)组成的数组 |
'odd' | 由奇数组成的数组 |
'integer' | 由整数值组成的数组 |
'real' | 由实数值组成的数组 |
'nonnegative' | 没有小于零的元素 |
'nonzero' | 没有等于零的元素 |
'positive' | 没有小于或等于零的元素 |
'decreasing' | 一列中的每个元素均小于其前面的元素,且无 NaN 元素。 |
'increasing' | 一列中的每个元素均大于其前面的元素,且无 NaN 元素。 |
'nondecreasing' | 一列中的每个元素均大于或等于其前面的元素,且无 NaN 元素。 |
'nonincreasing' | 一列中的每个元素均小于或等于其前面的元素,且无 NaN 元素。 |
数据类型: cell
funcName
— 用于验证的函数的名称
字符向量 | 字符串标量
用于验证的函数的名称,指定为字符向量或字符串标量。如果指定空字符向量 ''
或 <missing>
字符串,则 validateattributes
函数将忽略 funcName
输入。
数据类型: char
| string
varName
— 输入变量的名称
字符向量 | 字符串标量
输入变量的名称,指定为字符向量或字符串标量。如果指定空字符向量 ''
或 <missing>
字符串,则 validateattributes
函数将忽略 varName
输入。
数据类型: char
| string
argIndex
— 输入参数的位置
正整数
输入参数的位置,指定为正整数。
数据类型: double
扩展功能
C/C++ 代码生成
使用 MATLAB® Coder™ 生成 C 代码和 C++ 代码。
用法说明和限制:
某些错误消息是 MATLAB 错误消息的简化版本。
classes
、funcName
、varName
和argIndex
参数必须为常量。属性名称必须为常量。
在生成的代码中,错误消息中的数字格式可能与 MATLAB 中的格式不同。例如,以下是 MATLAB 中的错误消息:
Expected input to be an array with all of the values > 3.
以下是生成的代码中的错误消息:
Expected input to be an array with all of the values > 3.000000000000000e+00.
半精度数据类型支持
scalar
和real
属性。
基于线程的环境
使用 MATLAB® backgroundPool
在后台运行代码或使用 Parallel Computing Toolbox™ ThreadPool
加快代码运行速度。
此函数完全支持基于线程的环境。有关详细信息,请参阅在基于线程的环境中运行 MATLAB 函数。
GPU 数组
通过使用 Parallel Computing Toolbox™ 在图形处理单元 (GPU) 上运行来加快代码执行。
此函数完全支持 GPU 数组。有关详细信息,请参阅Run MATLAB Functions on a GPU (Parallel Computing Toolbox)。
分布式数组
使用 Parallel Computing Toolbox™ 在集群的组合内存中对大型数组进行分区。
此函数完全支持分布式数组。有关详细信息,请参阅Run MATLAB Functions with Distributed Arrays (Parallel Computing Toolbox)。
版本历史记录
在 R2007b 中推出
MATLAB 命令
您点击的链接对应于以下 MATLAB 命令:
请在 MATLAB 命令行窗口中直接输入以执行命令。Web 浏览器不支持 MATLAB 命令。
Select a Web Site
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:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- 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)