Main Content

Frequently Asked Questions About String Arrays

You can use string arrays to work with text throughout MathWorks® products. String arrays store pieces of text and provide a set of functions for working with text as data. You can index into, reshape, and concatenate strings arrays just as you can with arrays of any other type. For more information, see Create String Arrays.

In most respects, strings arrays behave like character vectors and cell arrays of character vectors. However, there are a few key differences between string arrays and character arrays that can lead to results you might not expect. For each of these differences, there is a recommended way to use strings that leads to the expected result.

Why Does Using Command Form With Strings Return An Error?

When you use functions such as the cd, dir, copyfile, or load functions in command form, avoid using double quotes. In command form, arguments enclosed in double quotes can result in errors. To specify arguments as strings, use functional form.

With command syntax, you separate inputs with spaces rather than commas, and you do not enclose input arguments in parentheses. For example, you can use the cd function with command syntax to change folders.

cd C:\Temp

The text C:\Temp is a character vector. In command form, all arguments are always character vectors. If you have an argument, such as a folder name, that contains spaces, then specify it as one input argument by enclosing it in single quotes.

cd 'C:\Program Files'

But if you specify the argument using double quotes, then cd throws an error.

cd "C:\Program Files"
Error using cd
Too many input arguments.

The error message can vary depending on the function that you use and the arguments that you specify. For example, if you use the load function with command syntax and specify the argument using double quotes, then load throws a different error.

load "myVariables.mat"
Error using load
Unable to read file '"myVariables.mat"': Invalid argument.

In command form, double quotes are treated as part of the literal text rather than as the string construction operator. If you wrote the equivalent of cd "C:\Program Files" in functional form, then it would look like a call to cd with two arguments.

cd('"C:\Program','Files"')

When specifying arguments as strings, use function syntax. All functions that support command syntax also support function syntax. For example, you can use cd with function syntax and input arguments that are double quoted strings.

cd("C:\Program Files")

Why Do Strings in Cell Arrays Return an Error?

When you have multiple strings, store them in a string array, not a cell array. Create a string array using square brackets, not curly braces. String arrays are more efficient than cell arrays for storing and manipulating text.

str = ["Venus","Earth","Mars"]
str = 1×3 string array
    "Venus"    "Earth"    "Mars"

Avoid using cell arrays of strings. When you use cell arrays, you give up the performance advantages that come from using string arrays. And in fact, most functions do not accept cell arrays of strings as input arguments, options, or values of name-value pairs. For example, if you specify a cell array of strings as an input argument, then the contains function throws an error.

C = {"Venus","Earth","Mars"}
C = 1×3 cell array
    {["Venus"]}    {["Earth"]}    {["Mars"]}
TF = contains(C,"Earth")
Error using contains
First argument must be a string array, character vector, or cell array of character vectors.

Instead, specify the argument as a string array.

str = ["Venus","Earth","Mars"];
TF = contains(str,"Earth");

Cell arrays can contain variables having any data types, including strings. It is still possible to create a cell array whose elements all contain strings. And if you already have specified cell arrays of character vectors in your code, then replacing single quotes with double quotes might seem like a simple update. However, it is not recommended that you create or use cell arrays of strings.

Why Does length() of String Return 1?

It is common to use the length function to determine the number of characters in a character vector. But to determine the number of characters in a string, use the strlength function, not length.

Create a character vector using single quotes. To determine its length, use the length function. Because C is a vector, its length is equal to the number of characters. C is a 1-by-11 vector.

C = 'Hello world';
L = length(C)
L = 11

Create a string with the same characters, using double quotes. Though it stores 11 characters, str is a 1-by-1 string array, or string scalar. If you call length on a string scalar, then the output argument is 1, no matter how many characters it stores.

str = "Hello World";
L = length(str)
L = 1

To determine the number of characters in a string, use the strlength function. For compatibility, strlength operates on character vectors as well. In both cases strlength returns the number of characters.

L = strlength(C)
L = 11
L = strlength(str)
L = 11

You also can use strlength on string arrays containing multiple strings and on cell arrays of character vectors.

The length function returns the size of the longest dimension of an array. For a string array, length returns the number of strings along the longest dimension of the array. It does not return the number of characters within strings.

Why Does isempty("") Return 0?

A string can have no characters at all. Such a string is an empty string. You can specify an empty string using an empty pair of double quotes.

L = strlength("")
L = 0

However, an empty string is not an empty array. An empty string is a string scalar that happens to have no characters.

sz = size("")
sz = 1×2    
     1     1

If you call isempty on an empty string, then it returns 0 (false) because the string is not an empty array.

tf = isempty("")
tf = logical
   0

However, if you call isempty on an empty character array, then it returns 1 (true). A character array specified as an empty pair of single quotes, '', is a 0-by-0 character array.

tf = isempty('')
tf = logical
   1

To test whether a piece of text has no characters, the best practice is to use the strlength function. You can use the same call whether the input is a string scalar or a character vector.

str = "";
if strlength(str) == 0
    disp('String has no text')
end
String has no text
chr = '';
if strlength(chr) == 0
    disp('Character vector has no text')
end
Character vector has no text

Why Does Appending Strings Using Square Brackets Return Multiple Strings?

You can append text to a character vector using square brackets. But if you add text to a string array using square brackets, then the new text is concatenated as new elements of the string array. To append text to strings, use the plus operator or the strcat function.

For example, if you concatenate two strings, then the result is a 1-by-2 string array.

str = ["Hello" "World"]
str = 1×2 string array
    "Hello"    "World"

However, if you concatenate two character vectors, then the result is a longer character vector.

str = ['Hello' 'World']
chr = 'HelloWorld'

To append text to a string (or to the elements of a string array), use the plus operator instead of square brackets.

str = "Hello" + "World"
str = "HelloWorld"

As an alternative, you can use the strcat function. strcat appends text whether the input arguments are strings or character vectors.

str = strcat("Hello","World")
str = "HelloWorld"

Whether you use square brackets, plus, or strcat, you can specify an arbitrary number of arguments. Append a space character between Hello and World.

str = "Hello" + " " + "World"
str = "Hello World"

See Also

| | | | | | | | | | | |

Related Topics