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
。
限制函数参数值
使用 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")
typename
— 要测试的数据类型的名称
字符向量 | 字符串标量
要测试的数据类型的名称,指定为字符向量或字符串标量。
示例: 如果 X
不具有基础类型 double
,则 mustBeUnderlyingType(X,"double")
会引发错误。
数据类型: char
| string
提示
mustBeUnderlyingType
用于属性和函数参数验证。
扩展功能
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 中推出
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)