mustBeUnderlyingType
说明
示例
验证参量具有指定的基础类型
使用 mustBeUnderlyingType
验证输入具有基础类型 double
。
创建一个 distributed
数组(需要 Parallel Computing Toolbox™),然后验证基础数据类型是 double
。
x = distributed(single(1:10));
mustBeUnderlyingType(x,"double")
Starting parallel pool (parpool) using the 'local' profile ... Connected to the parallel pool (number of workers: 6).
Value must have underlying type 'double'.
mustBeUnderlyingType
会引发错误,因为该分布式数组的基础类型是 single
。
您可以指定多个数据类型来对照进行检查。 (自 R2024a 起)定义类型为 double
的另一个分布式数组。
y = distributed(double(1:10));
当同时指定了 double
和 single
时,mustBeUnderlyingType
不会为 x
或 y
引发错误。
mustBeUnderlyingType(x,["double","single"]) mustBeUnderlyingType(y,["double","single"])
限制函数参量值
使用 mustBeUnderlyingType
来限制函数接受的输入参量值。您可以通过向验证输入参量的函数添加 arguments
代码块来实现这一点。
此函数声明一个输入参量。在 arguments
代码块中,要求输入的基础数据类型为 single
。
function y = mbSingle(input) arguments input {mustBeUnderlyingType(input,"single")} end disp("Input is class " + class(input) + ... " with underlying type " + underlyingType(input) + ".") end
使用 distributed
向量调用函数(需要 Parallel Computing Toolbox),该向量具有基础数据类型 single
。由于输入通过了参量验证,mbSingle
函数会输出关于类和基础类型的信息。
x = distributed(single(1:10)); mbSingle(x)
Starting parallel pool (parpool) using the 'local' profile ... Connected to the parallel pool (number of workers: 6). Input is class distributed with underlying type single.
输入参数
value
— 要验证的值
标量 | 数组 | 对象
要验证的值,指定为标量、数组或对象。
示例: mustBeUnderlyingType(magic(4),"single")
typenames
— 要测试的一个或多个数据类型的名称
字符串数组 | 字符向量 | 字符向量元胞数组
要测试的一个或多个数据类型的名称,指定为字符串数组、字符向量或字符向量元胞数组。
示例: 如果 X
不具有基础类型 double
或 single
,则 mustBeUnderlyingType(X,["double","single"])
会引发错误。
数据类型: char
| string
提示
mustBeUnderlyingType
用于属性和函数参量验证。
扩展功能
C/C++ 代码生成
使用 MATLAB® Coder™ 生成 C 代码和 C++ 代码。
GPU 数组
通过使用 Parallel Computing Toolbox™ 在图形处理单元 (GPU) 上运行来加快代码执行。
此函数完全支持 GPU 数组。有关详细信息,请参阅Run MATLAB Functions on a GPU (Parallel Computing Toolbox)。
分布式数组
使用 Parallel Computing Toolbox™ 在集群的组合内存中对大型数组进行分区。
此函数完全支持分布式数组。有关详细信息,请参阅Run MATLAB Functions with Distributed Arrays (Parallel Computing Toolbox)。
版本历史记录
在 R2020b 中推出R2024a: mustBeUnderlyingType
现在接受多个数据类型进行比较
typenames
输入参量现在接受多个数据类型进行比较,这些数据类型指定为字符串数组或字符向量元胞数组。
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)