mustBeNumeric
验证值为数值
说明
示例
验证输入为数值
验证运算结果为数值。
mustBeNumeric(5 < 10)
Values must be numeric.
小于关系运算符返回的是一个逻辑值。
限制属性值
此类将 Prop1
的值限制为数值。
classdef MyClass properties Prop1 {mustBeNumeric} end end
创建一个对象,并向其属性赋值。
obj = MyClass; obj.Prop1 = isprime(29);
Error setting 'Prop1' property of 'MyClass' class: Values must be numeric.
当您向属性赋值时,MATLAB® 会使用赋给属性的值调用 mustBeNumeric
。mustBeNumeric
将引发错误,因为赋给 Prop1
的值为逻辑值。
限制函数值
此函数将输入参数限制为数值向量。
function r = mbNumeric(x) arguments x (1,:) {mustBeNumeric} end p = [3 2 1]; r = polyval(p,x); end
使用字符向量调用该函数会导致 mustBeNumeric
引发错误。
x = '4 3 2';
r = mbNumeric(x);
Error using mbNumeric r = mbNumeric(x); ↑ Invalid input argument at position 1. Value must be numeric.
输入参数
提示
mustBeNumeric
用于属性和函数参数验证。
扩展功能
C/C++ 代码生成
使用 MATLAB® Coder™ 生成 C 代码和 C++ 代码。
版本历史记录
在 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)