MATLAB Programming Fundamentals - MathWorks

(やまだぃちぅ) #1
Check for Empty String Arrays

An empty string array is, in fact, an empty array—that is, an array that has at least one
dimension whose length is 0.

The recommended way to create an empty string array is to use the strings function,
specifying 0 as at least one of the input arguments. The isempty function returns 1 when
the input is an empty string array.

str = strings(0);
tf = isempty(str)

The strlength function returns a numeric array that is the same size as the input string
array. If the input is an empty string array, then strlength returns an empty array.

str = strings(0);
L = strlength(str)

Check for Missing Strings

String arrays also can contain missing strings. The missing string is the string equivalent
to NaN for numeric arrays. It indicates where a string array has missing values. The
missing string displays as <missing>, with no quotation marks.

You can create missing strings using the missing function. The recommended way to
check for missing strings is to use the ismissing function.

str = string(missing);
tf = ismissing(str)

NoteDo not check for missing strings by comparing a string to the missing string.

The missing string is not equal to itself, just as NaN is not equal to itself.

str = string(missing);
f = (str == missing)

Terminology for Character and String Arrays


MathWorks documentation uses these terms to describe character and string arrays. For
consistency, use these terms in your own documentation, error messages, and warnings.

6 Characters and Strings

Free download pdf