mustBeNonNan
验证值不是 NaN
说明
示例
验证值不为 NaN
使用 mustBeNonNan
验证任何数组元素均不为 NaN
。
A = 0./[-2 -1 0 1 2]; mustBeNonNan(A)
Value must not be NaN.
0
被 0
除等于 NaN
,因为数组值包含一个 NaN
元素,因而引发了错误。
限制属性值
此类将 Prop1
的值限制为非 NaN 值。
classdef MyClass properties Prop1 {mustBeNonNan} end end
创建一个对象,并向其 Prop1
赋值。
obj = MyClass; obj.Prop1 = 0./[-2 -1 0 1 2];
Error setting property 'Prop1' of class 'MyClass'. Value must not be NaN.
当您向属性赋值时,MATLAB 会使用赋给属性的值调用 mustBeNonNan
。mustBeNonNan
将引发错误,因为 0
被 0
除的结果是 NaN
。
限制函数参数值
此函数声明一个输入参数,该参数必须为不包含 NaN
元素的双精度向量。
function s = mbNonNan(x) arguments x (1,:) double {mustBeNonNan} end n = length(x); m = sum(x)/n; s = sqrt(sum((x-m).^2/n)); end
使用不满足 mustBeNonNan
要求的输入调用该函数会引发错误。
values = [12.7, 45.4, 98.9, NaN, 53.1]; s = mbNonNan(values);
Error using mbNonNan s = mbNonNan(values); ↑ Invalid input argument at position 1. Value must not be NaN.
输入参数
提示
mustBeNonNan
用于属性和函数参数验证。
扩展功能
C/C++ 代码生成
使用 MATLAB® Coder™ 生成 C 代码和 C++ 代码。
分布式数组
使用 Parallel Computing Toolbox™ 在集群的组合内存中对大型数组进行分区。
此函数完全支持分布式数组。有关详细信息,请参阅Run MATLAB Functions with Distributed Arrays (Parallel Computing Toolbox)。
版本历史记录
在 R2017a 中推出
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)