属性验证函数

1 用函数验证属性

在类定义中可以使用属性验证函数给属性值添加特定限制。验证函数接受可能的属性值作为变量,如果该值不满足指定要求,会生成一个错误。[大谦MATLAB,dqmatlab点com]

验证函数的语法格式为:

code.matlab
classdef MyClass
	properties
		Prop {fcn1,fcn2,...}=defaultValue
	end
end

如果验证函数需要输入参数,则属性和输入参数都要包括进来。例如,下面代码中,对于函数mustBeGreaterThan,需要一个限制值作为输入参数。验证函数要求属性值必须大于该限制值。将属性作为第一个变量传递,使用属性名,但是不要引号。属性定义要求Prop的值大于10。

code.matlab
properties
	Prop {mustBeGreaterThan(Prop,10)}
end

下面的类为每个属性指定验证函数。

code.matlab
classdef ValidatorFunction
	properties
		Data {mustBeNumeric, mustBeFinite}
		Interp {mustBeMember(Interp,{'linear','cubic','spline'})}='linear'
	end
end

代码中,Data必须是数值和有限值。Interp的值必须是3个可选项中的1个,指定1个默认值。

创建一个该类的默认对象,显示各属性的初值。

code.matlab
a=ValidatorFunction
a =
	ValidatorFunction with properties:
	Data: []
	Interp: 'linear'

下面给Data属性赋字符串,生成错误,指出赋的值必须是数值。

code.matlab
a.Data='cubic'
Error setting property 'Data' of class 'ValidatorFunction': Value must be numeric.

因为Data属性验证没包括数值类,所以不会将char型向量转换为数值型值。如果指定double类,如下所示,MATLAB会将char型向量转换为double型数组。

code.matlab
properties
	Data double {mustBeNumeric, mustBeFinite}
end

现在赋值char型向量不会生成错误了,因为MATLAB会将char型向量转换为double类。

code.matlab
a.Data='cubic'
a =
	ValidatorFunction with properties:
	Data: [99 117 98 105 99]
	Interp: 'linear'

赋值给Interp属性时值需要精确匹配。

code.matlab
a=ValidatorFunction;
a.Interp='cu'
Error setting property 'Interp' of class 'ValidatorFunction':
Value must be a member of this set
	linear
	cubic
	spline

使用枚举类则不需要精确匹配。例如,下面为Interp属性验证定义InterpMethod枚举类:

code.matlab
classdef InterpMethod
	enumeration
		linear
		cubic
		spline
	end
end

修改Interp属性验证,使用InterpMethod类。

code.matlab
classdef ValidatorFunction
	properties
		Data {mustBeNumeric, mustBeFinite}
		Interp InterpMethod
	end
end

使用下面的代码,虽然只给出'cu',但也能精确匹配到'cubic'。

code.matlab
a=ValidatorFunction;
a.Interp='cu'
a =
	ValidatorFunction with properties:
	Data: []
	Interp: cubic

2 MATLAB验证函数

表3-2列出了所有MATLAB验证函数、它们的意义和验证函数使用的MATLAB函数。

表3-2 验证函数

名 称 意 义 使用的MATLAB函数
mustBePositive(A) A > 0 gt, isreal, isnumeric, islogical
mustBeNonpositive(A) A <= 0 ge, isreal, isnumeric, islogical
mustBeFinite(A) A没有NaN和Inf元素 isfinite
mustBeNonNan(A) A没有NaN元素 isnan
mustBeNonnegative(A) A >= 0 ge, isreal, isnumeric, islogical
mustBeNegative(A) A < 0 lt, isreal, isnumeric, islogical
mustBeNonzero(A) A ~= 0 eq, isnumeric, islogical
mustBeGreaterThan(A,B) A > B gt, isscalar, isreal, isnumeric, islogical
mustBeLessThan(A,B) A < B lt, isreal, isnumeric, islogical
mustBeGreaterThanOrEqu al(A,B) A >= B ge, isreal, isnumeric, islogical
mustBeLessThanOrEqual( A,B) A <= B le, isreal, isnumeric, islogical
mustBeNonempty(A) A非空 isempty
mustBeNonsparse(A) A 没有稀疏元素 issparse
mustBeNumeric(A) A是数值 isnumeric
mustBeNumericOrLogical (A) A是数值型或逻辑型的 isnumeric, islogical
mustBeReal(A) A没有虚部 isreal
mustBeInteger(A) A==floor(A) isreal, isfinite, floor, isnumeric, islogical
mustBeMember(A,B) A与B的一个成员精确匹配 ismember

3 自定义验证函数

有时候MATLAB的验证函数不够用,需要创建自己的验证函数。可以在类文件中创建本地函数或把函数放在MATLAB路径上,以便让任何类都可以使用它。

例如,下面的ImData类使用本地函数定义一个验证器,它将Data属性的值限制在一个指定的数值范围内。

code.matlab
classdef ImData
	properties
		Data {mustBeNumeric, mustBeInRange(Data,[0,255])}=0
	end
end
function mustBeInRange(a,b)
	if any(a(:) < b(1)) || any(a(:) > b(2))
		error(['赋给Data属性的值不在指定范围内:',...
		num2str(b(1)),'...',num2str(b(2))])
	end
end

创建ImData类的一个实例时,MATLAB验证其默认值为数值型,在0到255之间,不为空。

code.matlab
a=ImData
a =
	ImData with properties:
	Data: 0

属性赋值按照从左到右的顺序调用验证器。赋一个char型向量给Data属性会生成一个错误,它由mustBeNumeric验证函数抛出。

code.matlab
a.Data='red'
Error setting property 'Data' of class 'ImData':
Value must be numeric.

赋一个超出范围的值时,会产生一个错误,它由验证函数mustBeInRange抛出。

code.matlab
a.Data=-1
Error setting property 'Data' of class 'ImData':
赋给Data属性的值不在指定范围内:0...255