MATLAB Programming Fundamentals - MathWorks

(やまだぃちぅ) #1

Represent Text with String Arrays


You also can store text in string arrays. Each element of a string array stores a 1-by-n
character vector.


Starting in R2017a, you can create strings using double quotes. MATLAB® displays
strings with double quotes.


str = "Welcome, friend"


str =
"Welcome, friend"


As an alternative, you can convert a character vector to a string using the string
function.


chr = 'Hello, world';
str = string(chr)


str =
"Hello, world"


str is a 1-by-1 string, or string scalar. To find the number of characters in a string, use
the strlength function.


whos str


Name Size Bytes Class Attributes


str 1x1 174 string


strlength(str)


ans = 12


You can store multiple pieces of text in a string array. Each element of the array can
contain a string of a different size.


str = ["Mercury","Gemini","Apollo";...
"Skylab","Skylab B","ISS"]


str = 2x3 string array
"Mercury" "Gemini" "Apollo"
"Skylab" "Skylab B" "ISS"


Represent Text with Character and String Arrays
Free download pdf