Matlab

Strcmp function in MatLab

The following article explains how to use the strcmp function in MATLAB® to compare two strings, cell arrays of character vectors, character vectors, or arrays of character strings.

The strcmp function is widely used and is part of the standard libraries of all popular programming languages in the world, and its execution mode is the same for all of them.

This article contains practical examples and screenshots that show how you can use this function with different types of input arguments. We will also show you some of the most common errors generated by this function, the corresponding error messages, and how to solve them to make programming easier.

Syntax

l = strcmp(s1, s2)

Description and examples

The function strcmp() function compares two character strings “s1” and “s2” and returns a logical result in “L”, true (1) if “s1” and “s2” are the same or false (0) if the strings “s1” and “s2” are different. With this function, the order of the inputs does not affect the result of the comparison.

The input arguments that this function accepts can be character strings, cell arrays of character vectors, character vectors, or character string arrays, and it supports international character sets. If both “s1” and “s2” are arrays of strings or cell arrays of character vectors, then “s1” and “s2” must have the same or a compatible size unless one of them is a scalar. If s1 and s2 are arrays of characters with multiple rows, then “s1” and “s2” can have a different number of rows.

How to compare two pure character strings.

The following example shows the simplest way to use this function when the input arguments are two string scalars. As shown below, the strings are the same in the first case, so the strcmp function returns a logical “1” in “L.” The character strings are different in the second case, and the strcmp function returns a logical “0” in “L.”

L = strcmp ("Equivalent","Equivalent")
L = strcmp ("is not equivalent","Equivalent")

If “s1” and “s2” are equal, the strcmp() function returns logical 1 in “L” and if “s1” and “s2” are not equal, it returns logical 0 in “L”.

How to compare two string scalars.

Next, we will look at how to call the strcmp() function by sending character scalars as input arguments instead of plain strings. We create vectors “s1” and “s2” to do this. The same applies to arrays of character vectors or arrays of strings.

s1 = 'Equivalent';
s2 = 'Equivalent';
L = strcmp(s1, s2)

As a result, it will return a logical 1.

How to use the strcmp function to search for a character string within an array of strings.

It is also possible to find the character string contained in “s1” within a cell array of character vectors or string arrays using the strcmp function. This compares the string contained in “s1” with each of the elements in “s2”.

s1 =  'Equivalent';
s2 = {'Equivalent', 'not Equivalent', 'not Equivalent';
      'Equivalent', 'not Equivalent', 'Equivalent';
      'Equivalent', 'not Equivalent', 'Equivalent'};
Lc = strcmp(s1, s2)

As a result of this, the strcmp function will return an “Lc” array of logical elements of true (1) or false (0) in the order and size of “s2”.

Lc=
      1   0   0
      1   0   1
      1   0   1

What happens if we want to compare two character strings with an array?

In the above example, the strcmp() function was sent with a single scalar as the input argument in “s1” to find the matching string in “s2”. In this example, “s1” is sent with two strings, so the compatible array size rule is not met.

s1 = {'Equivalent', 'not Equivalent'};
s2 = {'Equivalent', 'not Equivalent', 'not Equivalent';
      'Equivalent', 'not Equivalent', 'Equivalent';
      'Equivalent', 'not Equivalent', 'Equivalent'};
Lc = strcmp(s1, s2)

In this case, strcmp() will return the following error message:

“Error using strcmp Inputs must be the same size or either one can be a scalar.”

How to use the strcmp() function to compare two cell arrays of character vectors.

This example shows how you can use the strcmp function to compare two cell arrays of character vectors.

s1 =  {'Equivalent', 'Equivalent'; 'Equivalent', 'Equivalent'};
s2 =  {'Equivalent', 'Equivalent'; 'Is not equivalent', 'not Equivalent'};
lc = strcmp(s1,s2)

As a result, it will return a logical array of the same size and order as “s1” or “s2”.

lc=
      1   1
      0   0

Also, in this case, the input arguments must be cell arrays of character vectors of the same size; otherwise, the strcmp() function returns the following error message:

“Error using strcmp Inputs must be the same size or either one can be a scalar.”

How to compare two string arrays using the strcmp function.

Now we will see how to compare string arrays with the strcmp function. For this, we will create the arrays “s1” and “s2” with 3×3 elements.

s1 = ["Is equal", "Not Equivalent", "Equivalent"];
s2 = ["Is equal", "Equivalent", "Equivalent"];
Lc = strcmp(s1,s2)

As a result, it will return a logical array of the same size and order as “s1” or “s2”.

Lc=
      1   0   1

How to compare two character strings using relational operators in MATLAB.

Operators can also be used to perform operations on strings. The size and compatibility rules for arrays are the same as for strcmp.

Next, we will show you another way to compare character strings is by using the relational operator “==.” The syntax is as follows:
L = s1==s2.

s1 = ["Is equal", "Not Equivalent", "Equivalent"];
s2 = ["Is equal", "Equivalent", "Equivalent"];
Lc = s1==s2

Conclusion:

This article has explained how to use basic MATLAB functions to compare strings using the strcmp() function. We have also given some practical examples with pictures showing how to use these features. We have explained some of the most common errors with their respective messages and solutions to facilitate your programming. We have also detailed the input argument types supported by this function and the accepted data type. We hope you have found this MATLAB article useful. Check out other Linux Hint articles for more tips and information.

About the author

Julio Cesar

Julio Cesar is a 42 years old programmer with 8 years of experience in embedded systems development, 6 years developing firmware for user interfaces in C and C++. Additionally he has 2 years of experience developing scripts for network devices and 3 years as developer of high frequency PCB (Printed Circuit Board).