How to compare two strings in matlab

Strings are arrays of characters. We often use them to represent information to the user of our programs. Sometimes we use them to store information about the state of our program. When we do so, we will often want to "test" this state. To do so we need the strcmp function.

The function "strcmp" is used when comparing two strings for equality in Matlab. The strcmp function takes two input arguments (two strings) and returns either true or false, just like any boolean expression. Strcmp will only return true if every character of both strings is the same and they are the same length. In all other cases, strcmp will return false.

Here is an example of using strcmp:

strcmp('this', 'that') % strcmp is a function that returns true or false favorite_animal = input('what is your favorite animal? ','s'); favorite_dog = input('what is your favorite type of dog? ','s'); if ( strcmp( favorite_dog, favorite_animal ) ) fprintf('Your favorite animal is a type of dog!\n'); end

Example of Usage

Here is an example of using the input function and strcmp to gather valid input from the user:

input_value = input('is Jim your prof? (y/n) ','s'); % save the result, of the input_value being compared to the character 'y', % in the variable "jim_is_the_prof" jim_is_the_prof = strcmp(input_value, 'y'); % % Now do something based on this information. % if ( jim_is_the_prof ) % do something end

Last Warning:

MEMORIZE THIS: when comparing two strings, we always use the strcmp function, we should never use the double equals.

Back to Topics List

Input text, with each input specified as a character vector, a character array, a cell array of character vectors, or a string array. The order of the inputs does not affect the comparison results.

  • If both s1 and s2 are string arrays or cell arrays of character vectors, then s1 and s2 must be the same size, unless one of them is scalar.

  • If both s1 and s2 are character arrays with multiple rows, then s1 and s2 can have different numbers of rows.

  • When comparing a nonscalar cell array of character vectors or a string array to a multirow character array, the cell array or string array must be a column vector with the same number of rows as the character array.

Data Types: char | cell | string

How to compare two strings in matlab

편집: Stephen23 2015년 1월 6일

The documentation for both of strcmp and strcmpi clearly states that they accept cell arrays of strings as the inputs:

It may be that your indexing is not working properly. Can you please give more details about the variables s1 and s2: their class, size and contents would be useful. Or perhaps you have nested cell arrays? In order to help you any further, we need to know about those variables!

This might be worth reading too:

http://blogs.mathworks.com/loren/2006/06/21/cell-arrays-and-their-contents/

How to compare two strings in matlab

I don’t have your data so I cannot test this, but see if using only one subscript works:


Translated by

How to compare two strings in matlab

MathWorks

Accelerating the pace of engineering and science

MathWorks is the leading developer of mathematical computing software for engineers and scientists.

Compare text in character arrays and string arrays in different ways. 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.

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. You can create strings using double quotes.

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

Compare str1 and str2 for equality.

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 0 0 1 0 0 0

Compare a string array to a character vector. As long as one of the variables is a string array, you can make the comparison.

chr = 'Gemini'; TF = (str1 == chr)

TF = 2x3 logical array 0 1 0 0 0 0

Index into str1 with TF to extract the string elements that matched Gemini. You can use logical arrays to index into an array.

Compare for inequality using the ~= operator. Index into str1 to extract the elements that do not match 'Gemini'.

TF = 2x3 logical array 1 0 1 1 1 1

ans = 5x1 string "Mercury" "Skylab" "Skylab B" "Apollo" "International Space Station"

Compare two nonscalar string arrays. When you compare two nonscalar arrays, they must be the same size.

str2 = ["Mercury","Mars","Apollo";... "Jupiter","Saturn","Neptune"]; TF = (str1 == str2)

TF = 2x3 logical array 1 0 1 0 0 0

Index into str1 to extract the matches.

ans = 2x1 string "Mercury" "Apollo"

Compare String Arrays with Other Relational Operators

You can also compare strings with the relational operators >, >=, <, and <=. Strings that start with uppercase letters come before strings that start with lowercase letters. For example, the string "ABC" is less than "abc". Digits and some punctuation marks also come before letters.

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

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.

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.

ans = 2x3 string "Jupiter" "Mars" "Apollo" "Mercury" "Saturn" "Neptune"

To sort the elements in each row, sort str2 along the second dimension.

ans = 2x3 string "Apollo" "Mars" "Mercury" "Jupiter" "Neptune" "Saturn"

Compare Character Vectors

You can compare character vectors and cell arrays of character vectors to each other. Use the strcmp function to compare two character vectors, or strncmp to compare the first N characters. You also can use strcmpi and strncmpi for case-insensitive comparisons.

Compare two character vectors with the strcmp function. chr1 and chr2 are not equal.

chr1 = 'hello'; chr2 = 'help'; TF = strcmp(chr1,chr2)

Note that the MATLAB strcmp differs from the C version of strcmp. The C version of strcmp returns 0 when two character arrays are the same, not when they are different.

Compare the first two characters with the strncmp function. TF is 1 because both character vectors start with the characters he.

TF = strncmp(chr1,chr2,2)

Compare two cell arrays of character vectors. strcmp returns a logical array that is the same size as the cell arrays.

C1 = {'pizza'; 'chips'; 'candy'}; C2 = {'pizza'; 'chocolate'; 'pretzels'}; strcmp(C1,C2)

ans = 3x1 logical array 1 0 0

Inspect Characters in String and Character Arrays

You can inspect the characters in string arrays or character arrays with the isstrprop, isletter, and isspace functions.

  • The isstrprop inspects characters in either string arrays or character arrays.

  • The isletter and isspace functions inspect characters in character arrays only.

Determine which characters in a character vector are space characters. isspace returns a logical vector that is the same size as chr.

chr = 'Four score and seven years ago'; TF = isspace(chr)

TF = 1x30 logical array 0 0 0 0 1 0 0 0 0 0 1 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0

The isstrprop function can query characters for many different traits. isstrprop can determine whether characters in a string or character vector are letters, alphanumeric characters, decimal or hexadecimal digits, or punctuation characters.

Determine which characters in a string are punctuation marks. isstrprop returns a logical vector whose length is equal to the number of characters in str.

str = "A horse! A horse! My kingdom for a horse!"

str = "A horse! A horse! My kingdom for a horse!"

ans = 1x41 logical array 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1

Determine which characters in the character vector chr are letters.

ans = 1x30 logical array 1 1 1 1 0 1 1 1 1 1 0 1 1 1 0 1 1 1 1 1 0 1 1 1 1 1 0 1 1 1