mustBeA
说明
mustBeA(
将 value
,classNames
)value
与 classNames
中的类名称列表进行比较,如果 value
的类不是某个命名类或某个命名类的子类,则会引发错误。此函数不返回值。
支持的类:所有 MATLAB® 类
示例
确定值是否属于特定类型
验证值是标量,或者是字符串或 uint8
类型的数组。
a = 'red'; mustBeA(a,["string","uint8"])
Value must be of the following types: 'string' or 'uint8'.
传递字符向量无效,因为 mustBeA
将文本值限制为仅字符串。
b = [1 0 0]; mustBeA(b,["string","uint8"])
Value must be of the following types: 'string' or 'uint8'.
传递双精度整数数组无效,因为 mustBeA
将数值限制为仅 uint8
。
有效子类
子类与超类满足 "isa" 关系。因此,作为指定类之一的子类的值有效。
定义 uint8
的一个子类。
classdef ColorSpec < uint8 methods function obj = ColorSpec(c) if nargin == 0 c = uint8(0); end obj = obj@uint8(c); end end end
创建 ColorSpec
类的一个对象。
a = ColorSpec([1 0 0])
a = 1×3 ColorSpec: uint8 data: 1 0 0
ColorSpec
对象包含 uint8
数据。
用 mustBeA
测试 ColorSpec
对象的有效性。以下语句不会生成错误。
mustBeA(a,["string","uint8"])
输入参数
value
— 要验证的值
标量 | 数组
要验证的值,指定为标量或任何 MATLAB 或用户定义的类型的数组。
classNames
— 一个或多个类的名称
数据类型或类的名称
一个或多个类的名称,指定为字符串或字符向量。
示例: ["double", "single"]
提示
mustBeA
用于属性和函数参数验证。
扩展功能
C/C++ 代码生成
使用 MATLAB® Coder™ 生成 C 代码和 C++ 代码。
用法说明和限制:
代码生成不支持字符串数组。要将两个或多个类的名称传递给
mustBeA
,请使用字符向量元胞数组。
版本历史记录
在 R2020b 中推出
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)