| MATLAB Function Reference | ![]() |
TF = strcmp('str1', 'str2')
TF = strcmp('str', C)
TF = strcmp(C1, C2)
Each of these syntaxes apply to both strcmp and strcmpi. The strcmp function is case sensitive in matching strings, while strcmpi is not.
Although the following descriptions show only strcmp, they apply to strcmpi as well. The two functions are the same except that strcmpi compares strings without sensitivity to letter case:
TF = strcmp('str1', 'str2') compares the strings str1 and str2 and returns logical 1 (true) if they are identical, and returns logical 0 (false) otherwise. str1 and str2 can be character arrays of any dimension, but strcmp does not return true unless the sizes of both arrays are equal, and the contents of the two arrays are the same.
TF = strcmp('str', C) compares string str to the each element of cell array C, where str is a character vector (or a 1-by-1 cell array) and C is a cell array of strings. The function returns TF, a logical array that is the same size as C and contains logical 1 (true) for those elements of C that are a match, and logical 0 (false) for those elements that are not. The order of the first two input arguments is not important.
TF = strcmp(C1, C2) compares each element of C1 to the same element in C2, where C1 and C2 are equal-size cell arrays of strings. Input C1 or C2 can also be a character array with the right number of rows. The function returns TF, a logical array that is the same size as C1 and C2, and contains logical 1 (true) for those elements of C1 and C2 that are a match, and logical 0 (false) for those elements that are not.
These functions are intended for comparison of character data. When used to compare numeric data, they return logical 0.
Any leading and trailing blanks in either of the strings are explicitly included in the comparison.
The value returned by strcmp and strcmpi is not the same as the C language convention.
strcmp and strcmpi support international character sets.
Perform a simple comparison of two strings:
strcmp('Yes', 'No')
ans =
0
strcmp('Yes', 'Yes')
ans =
1Create 3 cell arrays of strings:
A = {'MATLAB','SIMULINK'; ...
'Toolboxes', 'The MathWorks'};
B = {'Handle Graphics', 'Real Time Workshop'; ...
'Toolboxes', 'The MathWorks'};
C = {'handle graphics', 'Signal Processing'; ...
' Toolboxes', 'The MATHWORKS'};Compare cell arrays A and B with sensitivity to case:
strcmp(A, B)
ans =
0 0
1 1Compare cell arrays B and C without sensitivity to case. Note that 'Toolboxes' doesn't match because of the leading space characters in C{2,1} that do not appear in B{2,1}:
strcmpi(B, C)
ans =
1 0
0 1Compare a string vector to a cell array of strings, a string vector to a string array, and a string array to a cell array of strings.
Start by creating a cell array of strings, a string array containing the same strings (plus padding space characters), and a string vector containing one of the strings (plus padding).
cellArr = {'It was the best of times'; ...
'it was the worst of times'; ...
'it was the age of wisdom'; ...
'it was the age of foolishness'};
strArr = char(cellArr);
strVec = strArr(3,:)
strVec =
it was the age of wisdom
Remove the space padding from the string vector and compare it to the cell array. The MATLAB® software compares the string with each row of the cell array, finding a match on the third row:
strcmp(deblank(strVec), cellArr)
ans =
0
0
1
0
Compare the string vector with the string array. Unlike the case above, MATLAB does not compare the string vector with each row of the string array. It compares the entire contents of one against the entire contents of the other:
strcmp(strVec, strArr)
ans =
0
Lastly, compare each row of the four-row string array against the same rows of the cell array. MATLAB finds them all to be equivalent. Note that in this case you do not have to remove the space padding from the string array:
strcmp(strArr, cellArr)
ans =
1
1
1
1strncmp, strncmpi, strmatch, strfind, findstr, regexp, regexpi, regexprep, regexptranslate
![]() | strcat | stream2 | ![]() |
| © 1984-2008- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |