Main Content

本页翻译不是最新的。点击此处可查看最新英文版本。

mustBeUnderlyingType

验证值具有指定的基础类型

自 R2020b 起

说明

示例

如果 value 没有基础数据类型 typename,则 mustBeUnderlyingType(value,typename) 会引发错误。此函数不返回值。

mustBeUnderlyingType 调用以下函数来确定输入是否具有指定的基础类型:

支持的类:所有 MATLAB®

示例

全部折叠

使用 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.

输入参数

全部折叠

要验证的值,指定为标量、数组或对象。

示例: mustBeUnderlyingType(magic(4),"single")

要测试的数据类型的名称,指定为字符向量或字符串标量。

示例: 如果 X 不具有基础类型 double,则 mustBeUnderlyingType(X,"double") 会引发错误。

数据类型: char | string

提示

  • mustBeUnderlyingType 用于属性和函数参数验证。

扩展功能

C/C++ 代码生成
使用 MATLAB® Coder™ 生成 C 代码和 C++ 代码。

版本历史记录

在 R2020b 中推出