添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

validateattributes

检查数组的有效性

说明

示例

validateattributes( A , classes , attributes ) 验证数组 A 是否属于至少一个指定的类(或其子类)并具有所有指定的属性。如果 A 不符合条件,MATLAB ® 将引发错误,并显示一条格式化的错误消息。否则, validateattributes 将完成并且不显示任何输出。

示例

validateattributes( A , classes , attributes , argIndex ) 将输入位置包含在函数参量列表中作为任何生成的错误消息的一部分。

示例

validateattributes( A , classes , attributes , funcName ) 将指定的函数名称包括在生成的错误标识符中。

示例

validateattributes( A , classes , attributes , funcName , varName ) 将指定的变量名称包括在生成的错误消息中。

示例

validateattributes( A , classes , attributes , funcName , varName , argIndex ) 将指定的信息包括在生成的错误消息或标识符中。

示例

全部折叠

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'})
     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 检查输入参数的自定义函数,并使用 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

调用该函数并且第三个输入为非数值。

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:
Instead its type was double.
Error in findVolume (line 2)
validateattributes(shape,{'char'},{'nonempty'},mfilename,'Shape',1)

函数名称作为错误标识符的一部分。

MException.last.identifier
ans =
MATLAB:findVolume:invalidType

输入参数

全部折叠

输入,指定为任何类型的数组。

数据类型: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | logical | char | string | struct | cell | function_handle
复数支持:

有效属性类型,指定为字符向量、字符向量元胞数组或字符串数组。 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
'< class_name >' 任何其他类名

数据类型: cell | string

有效属性,指定为元胞数组或字符串数组。

某些属性还需要数值,例如,用于指定 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

用于验证的函数的名称,指定为字符向量或字符串标量。如果指定空字符向量 '' <missing> 字符串,则 validateattributes 函数将忽略 funcName 输入。

数据类型: char | string

输入变量的名称,指定为字符向量或字符串标量。如果指定空字符向量 '' <missing> 字符串,则 validateattributes 函数将忽略 varName 输入。

数据类型: char | string

输入参量的位置,指定为正整数。

数据类型: double

扩展功能

版本历史记录

在 R2007b 中推出