Main Content

mustBeText

Validate that value is string array, character vector, or cell array of character vectors

Since R2020b

Description

mustBeText(value) throws an error if value is not a string array, character vector, or cell array of character vectors. An empty 0x0 char array '', empty string "", and missing strings are special cases that also counts as text. This function does not return a value.

example

Examples

collapse all

Create an array of numbers notText, and then use mustBeText to validate the value. mustBeText throws an error since the array does not contain text.

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

Use mustBeText to restrict the input argument values that are accepted by a function. You can accomplish this by adding an arguments block to the function that validates the input arguments.

This function restricts the value of the argument textInput to text values.

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

Call the function with a numeric input argument. MATLAB® calls mustBeText on the value being assigned to the argument. mustBeText issues an error because the value 2 is not text.

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

Input Arguments

collapse all

Value to validate, specified as a scalar or array. If value, is not a string scalar or array, character array, or cell array of character vectors mustBeText will throw an error.

Example: mustBeText('foo')

Extended Capabilities

C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.

Version History

Introduced in R2020b