"ABC" < "abc"
ans = logical
1
Compare a string array that contains names to another name with the > operator. The names
Sanchez, de Ponte, and Nash come after Matthews, because S, d, and N all are greater than M.
str = ["Sanchez","Jones","de Ponte","Crosby","Nash"];
TF = (str > "Matthews")
TF = 1x5 logical array
1
0
1
0
1
str(TF)
ans = 1x3 string
"Sanchez"
"de Ponte"
"Nash"
Sort String Arrays
You can sort string arrays. MATLAB® stores characters as Unicode® using the UTF-16 character
encoding scheme. Character and string arrays are sorted according to the UTF-16 code point order.
For the characters that are also the ASCII characters, this order means that uppercase letters come
before lowercase letters. Digits and some punctuation also come before letters.
Sort the string array str.
sort(str)
ans = 1x5 string
"Crosby"
"Jones"
"Nash"
"Sanchez"
"de Ponte"
Sort a 2-by-3 string array. The sort function sorts the elements in each column separately.
sort(str2)
ans = 2x3 string
"Jupiter"
"Mars"
"Apollo"
"Mercury"
"Saturn"
"Neptune"
To sort the elements in each row, sort str2 along the second dimension.
sort(str2,2)
ans = 2x3 string
"Apollo"
"Mars"
"Mercury"
"Jupiter"
"Neptune"
"Saturn"
6 Characters and Strings
6-34
ans = logical
1
Compare a string array that contains names to another name with the > operator. The names
Sanchez, de Ponte, and Nash come after Matthews, because S, d, and N all are greater than M.
str = ["Sanchez","Jones","de Ponte","Crosby","Nash"];
TF = (str > "Matthews")
TF = 1x5 logical array
1
0
1
0
1
str(TF)
ans = 1x3 string
"Sanchez"
"de Ponte"
"Nash"
Sort String Arrays
You can sort string arrays. MATLAB® stores characters as Unicode® using the UTF-16 character
encoding scheme. Character and string arrays are sorted according to the UTF-16 code point order.
For the characters that are also the ASCII characters, this order means that uppercase letters come
before lowercase letters. Digits and some punctuation also come before letters.
Sort the string array str.
sort(str)
ans = 1x5 string
"Crosby"
"Jones"
"Nash"
"Sanchez"
"de Ponte"
Sort a 2-by-3 string array. The sort function sorts the elements in each column separately.
sort(str2)
ans = 2x3 string
"Jupiter"
"Mars"
"Apollo"
"Mercury"
"Saturn"
"Neptune"
To sort the elements in each row, sort str2 along the second dimension.
sort(str2,2)
ans = 2x3 string
"Apollo"
"Mars"
"Mercury"
"Jupiter"
"Neptune"
"Saturn"
6 Characters and Strings
6-34
