MATLAB Programming Fundamentals - MathWorks

(やまだぃちぅ) #1

Compare Text


Compare text in character arrays and string arrays in different ways. String arrays were
introduced in R2016b. You can compare string arrays and character vectors with
relational operators and with the strcmp function. You can sort string arrays using the
sort function, just as you would sort arrays of any other type. MATLAB® also provides
functions to inspect characters in pieces of text. For example, you can determine which
characters in a character vector or string array are letters or space characters.

Compare String Arrays for Equality

You can compare string arrays for equality with the relational operators == and ~=. When
you compare string arrays, the output is a logical array that has 1 where the relation is
true, and 0 where it is not true.

Create two string scalars. Starting in R2017a, you can create strings using double quotes.

str1 = "Hello";
str2 = "World";
str1,str2

str1 =
"Hello"

str2 =
"World"

Compare str1 and str2 for equality.

str1 == str2

ans = logical
0

Compare a string array with multiple elements to a string scalar.

str1 = ["Mercury","Gemini","Apollo";...
"Skylab","Skylab B","International Space Station"];
str2 = "Apollo";
str1 == str2

ans = 2x3 logical array

Compare Text
Free download pdf