Introduction to Strcmp Matlab
‘Strcmp’ command stands for string comparison. This command used to compare two or more strings . ‘strcmp’ command gives result in form of ‘1’ and ‘0’.This command is applicable for all types of data formats such as single data, arrays, vectors, or multi-dimensional data. accordingly, it gives the result in the form of ones and zeros. Strcmp function is case sensitive so that capital letters and small letters considered as different letters .to To resolve such problems there is one more function ‘strcmpi’. In strcmpi function both the cases of letters considered as same.
Syntax:
Strcmp(string1,string2)
Strcmp (name of first string, name of second string)
Strcmpi(string1,string2)
Strcmpi (name of first string, name of second string)
How to Use Strcmp in Matlab?
To use command or function strcmp and strcmpi we need at least two inputs in the form of string for comparison purposes. As we know ’strcmp’ command gives result in form of ‘1’ and ‘0’. if the output is ‘0’ that means false result and if the output is ‘1’ that means true result. The true results indicate both the strings are exact matches and the false result indicates both the strings are not the same that is different.
Example #1
Let us consider two strings with variable string 1 and string 2.string 1 is ‘hi’ and string 2 is also ‘hi’ so that we will get the true result after applying function strcmp. Therefore output will be 1 in example 1(a). For example 1(b) inputs are different. the first string is ‘hi’ and the second string is ‘bye’. Therefore the output of 1(b) will be 0.
Example 1 ( a ):
clc ;
clear all ;
string1 =' hi '
string2 = ' hi'
strcmp(string1,string2)
Output:
Example 1 ( b ):
clc ;
clear all ;
string1 = ' hi '
string2 = ' bye '
strcmp(string1,string2)
Output:
Example #2
In this example, we assigned five different strings. String 1 is ‘ string comp ’ and string two is exactly the same as string 1 so the result will be 1 that is true. string 3 is ‘stringcomp’, in this string letters are the same but there is no space between two words that is why when we compare string 1 and string 3 it will give false results. then string 4 is ‘STRING COMP’, this string is the same as string 1but all the letters are upper case . strcmp is case sensitive function so it will consider both the strings are different though the strings are same. String 5 is ‘comp string’, in this string letters are the same as well as case is the same but the arrangement of letters is different so it will give again false results.
clc ;
clear all ;
string1 = ' string comp '
string2 = ' string comp '
string3 = ' stringcomp '
string4 = ' STRING COMP '
string5 = ' comp string '
strcmp ( string1,string2)
strcmp(string1,string3)
strcmp(string1,string4)
strcmp(string1,string5)
Output:
Example #3
In example 3(a), there are two strings, the first string is one dimensional with data ‘add’, only one element is present in it. And the second string is an array or vector of strings. String 2 is ‘add’ , ‘sub’, ‘div’ and ‘mul’. Strcmp function will compare the first string with all elements of the second array individually And it will produce the result in the form of vector only. In example 3(b) string 1 is ‘add’ and the second input is a multi-dimensional matrix of strings. String 2 is {‘per’, ‘sub’, ‘mod’; ‘div’, ‘mul’, ‘add’}, it has two rows and three columns. So it will produce a result in a form matrix with dimensions two rows and three columns.
Example 3(a):
clc ;
clear all ;
string1 = 'add'
string2 = {'add', 'sub', 'div', 'mul'}
strcmp(string1,string2)
Output:
Example 3 (b):
clc ;
clear all ;
string1 = ' add '
string2 = {'per', 'sub', 'mod' ; 'div' ,'mul', 'add'}
strcmp(string1,string2)
Output:
Example #4
In this example, we have used strcmpi function along with the strcmp function. there are two strings, string 1 is ‘E’ .and string 2 is a multidimensional matrix which has random alphabets with ‘e’ and ‘E’ .if we compare both the strings by using strcmp function then we will get output matrix for letter ‘E’ an ‘e’ is zero, and if we compare both the strings by using function strcmpi then we will get the result as 1.
clc ;
clear all ;
string1 = 'E'
string2 = {'A', 'e', ' E' ; 'D' ,'e', 'E' ; 'e', 'E', 'V',}
strcmp(string1,string2)
strcmpi(string1,string2)
Output:
Conclusion
Strcmp is one of the most powerful functions in Matlab because it provides expressive nature to the program. If the strings are in different case upper cases or lower cases then in Matlab there is one more feature which is strcmpi. Strcmpi ignores the case of a letter and performs comparison directly. Strcmpi function is extension of strcmp function.
Recommended Articles
This is a guide to Strcmp Matlab. Here we also discuss the introduction and how does strcmp Matlab is done along with its different examples and its code implementation. You may also have a look at the following articles to learn more –