EDUCBA

EDUCBA

MENUMENU
  • Explore
    • Lifetime Membership
    • All in One Bundles
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Login
Home Data Science Data Science Tutorials Matlab Tutorial Strcmp Matlab

Strcmp Matlab

Priya Pedamkar
Article byPriya Pedamkar

Strcmp Matlab

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:

ADVERTISEMENT
Popular Course in this category
MATLAB Course Bundle - 5 Courses in 1 | 3 Mock Tests

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

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:

Strcmp Matlab-1.1

Example 1 ( b ):

clc ;
clear all ;
string1 = ' hi '
string2 = ' bye '
strcmp(string1,string2)

Output:

Strcmp Matlab-1.2

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:

Strcmp Matlab-1.3

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:

Output-1.4

Example 3 (b):

clc ;
clear all ;
string1 = ' add '
string2 = {'per', 'sub', 'mod' ; 'div' ,'mul', 'add'}
strcmp(string1,string2)

Output:

Output-1.5

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:

Output-1.6

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 –

  1. Switch Statement in Matlab
  2. Matlab Commands
  3. Matlab sym()
  4. Heaviside MATLAB
ADVERTISEMENT
SPSS Course Bundle - 14 Courses in 1 | 5 Mock Tests
34+ Hours of HD Videos
14 Courses
5 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
ADVERTISEMENT
MICROSOFT AZURE Course Bundle - 15 Courses in 1 | 12 Mock Tests
63+ Hour of HD Videos
15 Courses
12 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
ADVERTISEMENT
HADOOP Course Bundle - 32 Courses in 1 | 4 Mock Tests
125+ Hour of HD Videos
32 Courses
4 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
ADVERTISEMENT
INFORMATICA Course Bundle - 7 Courses in 1
47+ Hours of HD Videos
7 Courses
Verifiable Certificate of Completion
Lifetime Access
4.5
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2023 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

🚀 Extended Cyber Monday Price Drop! All in One Universal Bundle (3700+ Courses) @ 🎁 90% OFF - Ends in ENROLL NOW