Main Content

mustBeText

验证值是字符串数组、字符向量或字符向量元胞数组

自 R2020b 起

说明

示例

如果 value 不是字符串数组、字符向量或字符向量元胞数组,则 mustBeText(value) 会引发错误。空的 0×0 字符数组 ''、空字符串 "" 以及缺失字符串均为特殊情况,也记为文本。此函数不返回值。

示例

全部折叠

创建一个数值数组 notText,然后使用 mustBeText 验证值。mustBeText 会引发错误,因为该数组不包含文本。

notText = [1 2 3];
mustBeText(notText)
Value must be a character vector, string array, or cell array of character vectors.

使用 mustBeText 来限制函数接受的输入参量值。您可以通过向验证输入参量的函数添加参量代码块来实现这一点。

此函数将参量 textInput 的值限制为文本值。

function MyFunction(textInput)
   arguments
      textInput {mustBeText}
   end
end

用数值输入参量调用该函数。MATLAB® 对分配给参量的值调用 mustBeTextmustBeText 会发出错误,因为值 2 不是文本。

MyFunction(2)
Error using MyFunction
 MyFunction(2)
            ↑
Invalid argument at position 1. Value must be a character vector, string array, or cell array of character vectors.

输入参数

全部折叠

要验证的值,指定为标量或数组。如果 value 不是字符串标量或数组、字符数组或字符向量元胞数组,mustBeText 将引发错误。

示例: mustBeText('foo')

扩展功能

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

版本历史记录

在 R2020b 中推出