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");
Before R2016b, the term "cell array of strings" meant a cell array whose elements all contain
character vectors. But it is more precise to refer to such cell arrays as "cell arrays of character
vectors," to distinguish them from string arrays.
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
6 Characters and Strings
6-60
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");
Before R2016b, the term "cell array of strings" meant a cell array whose elements all contain
character vectors. But it is more precise to refer to such cell arrays as "cell arrays of character
vectors," to distinguish them from string arrays.
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
6 Characters and Strings
6-60
