Section: String Functions
p = strcmp(x,y)
where x and y are two strings. Returns true if x
and y are the same size, and are equal (as strings). Otherwise,
it returns false.
In the second form, strcmp can be applied to a cell array of
strings. The syntax for this form is
p = strcmp(cellstra,cellstrb)
where cellstra and cellstrb are cell arrays of a strings
to compare. Also, you can also supply a character matrix as
an argument to strcmp, in which case it will be converted
via cellstr (so that trailing spaces are removed), before being
compared.
--> x1 = 'astring'; --> x2 = 'bstring'; --> x3 = 'astring'; --> strcmp(x1,x2) ans = <logical> - size: [1 1] 0 --> strcmp(x1,x3) ans = <logical> - size: [1 1] 1
Here we use a cell array strings
--> x = {'astring','bstring',43,'astring'}
x =
<cell array> - size: [1 4]
Columns 1 to 3
astring bstring [43]
Columns 4 to 4
astring
--> p = strcmp(x,'astring')
p =
<logical> - size: [1 4]
Columns 1 to 4
1 0 0 1
Here we compare two cell arrays of strings
--> strcmp({'this','is','a','pickle'},{'what','is','to','pickle'})
ans =
<logical> - size: [1 4]
Columns 1 to 4
0 1 0 1
Finally, the case where one of the arguments is a matrix string
--> strcmp({'this','is','a','pickle'},['peter ';'piper ';'hated ';'pickle']);