📖 Scenario: You are working on a simple program to check if two words are the same. This is useful when you want to compare user input with a correct answer or check if two labels match.
🎯 Goal: Build a MATLAB script that compares two strings and tells if they are exactly the same or not.
📋 What You'll Learn
Create two string variables with exact values
Create a variable to hold the comparison result
Use the strcmp function to compare the two strings
Print the comparison result
💡 Why This Matters
🌍 Real World
String comparison is useful in many programs where you need to check if user input matches expected answers, like password checks or form validations.
💼 Career
Understanding string comparison is important for software testing, data validation, and building interactive applications.
Progress0 / 4 steps
1
Create two string variables
Create two string variables called word1 and word2 with the exact values 'hello' and 'Hello' respectively.
MATLAB
Hint
Use single quotes to create strings in MATLAB, like word1 = 'hello';
2
Create a variable for comparison result
Create a variable called areEqual to store the result of comparing word1 and word2 using the strcmp function.
MATLAB
Hint
Use strcmp(word1, word2) to compare the two strings exactly.
3
Check the comparison result
Use an if statement to check if areEqual is true. Inside the if, create a variable called message with the value 'The words are the same.'. Otherwise, set message to 'The words are different.'.
MATLAB
Hint
Use if areEqual to check the boolean and assign message accordingly.
4
Display the result message
Use the disp function to print the value of the message variable.
MATLAB
Hint
Use disp(message) to show the message on the screen.
Practice
(1/5)
1. Which MATLAB function is used to compare two strings with case sensitivity?
easy
A. strcmpi
B. strcmp
C. strcomp
D. strcompare
Solution
Step 1: Understand string comparison functions
MATLAB provides strcmp for case-sensitive string comparison and strcmpi for case-insensitive comparison.
Step 2: Identify the correct function for case-sensitive comparison
strcmp compares strings exactly, including letter case, so it is the correct choice.
Final Answer:
strcmp -> Option B
Quick Check:
Case-sensitive comparison = strcmp [OK]
Hint: Use strcmp for exact case-sensitive string checks [OK]
Common Mistakes:
Confusing strcmp with strcmpi
Using non-existent functions like strcomp
Assuming strcmpi is case-sensitive
2. Which of the following is the correct syntax to compare two strings s1 and s2 ignoring case in MATLAB?
easy
A. strcmpi(s1 == s2)
B. strcmp(s1, s2)
C. strcmpi(s1, s2)
D. strcmp(s1 == s2)
Solution
Step 1: Recall syntax for case-insensitive comparison
strcmpi compares two strings ignoring case and takes two string inputs as separate arguments.
Step 2: Identify correct syntax
strcmpi(s1, s2) uses strcmpi with two string arguments correctly. Options C and D incorrectly use '==' inside the function, which is invalid.
Final Answer:
strcmpi(s1, s2) -> Option C
Quick Check:
Case-insensitive syntax = strcmpi(s1, s2) [OK]
Hint: Use strcmpi(s1, s2) for ignoring case in string comparison [OK]
Common Mistakes:
Using '==' inside strcmp or strcmpi
Mixing up strcmp and strcmpi
Passing strings as one argument with '=='
3. What is the output of the following MATLAB code?
strcmp compares strings with case sensitivity. "Hello" and "hello" differ in case.
Step 2: Evaluate strcmp result
Since the strings differ in case, strcmp returns logical 0 (false), which disp prints as 0.
Final Answer:
0 -> Option D
Quick Check:
Case-sensitive mismatch = 0 [OK]
Hint: strcmp returns 0 if cases differ exactly [OK]
Common Mistakes:
Assuming strcmp ignores case
Expecting output '1' for different case
Confusing output with string 'true'
4. Identify the error in this MATLAB code snippet for case-insensitive string comparison:
s1 = "Test"; s2 = "test"; if strcmpi(s1 == s2) disp('Match'); end
medium
A. Using '==' inside strcmpi causes an error
B. Missing semicolon after disp statement
C. strcmpi cannot compare strings
D. Strings must be converted to char arrays first
Solution
Step 1: Analyze the use of '==' inside strcmpi
The expression s1 == s2 compares strings element-wise, returning a logical array, not a string.
Step 2: Understand strcmpi input requirements
strcmpi expects two string inputs, not a logical array. Using '==' inside causes a syntax or runtime error.
Final Answer:
Using '==' inside strcmpi causes an error -> Option A
Quick Check:
strcmpi needs strings, not logical arrays [OK]
Hint: Do not use '==' inside strcmpi; pass strings directly [OK]
Common Mistakes:
Using '==' inside strcmpi
Thinking strcmpi compares logical arrays
Ignoring input type requirements
5. You want to compare two cell arrays of strings C1 = {'Apple', 'Banana', 'Cherry'} and C2 = {'apple', 'banana', 'cherry'} ignoring case. Which MATLAB code correctly returns a logical array indicating matches?
hard
A. cellfun(@strcmpi, C1, C2)
B. strcmpi(C1{:}, C2{:})
C. cellfun(@strcmp, C1, C2)
D. strcmp(C1, C2)
Solution
Step 1: Understand cell array string comparison
To compare each pair of strings in cell arrays element-wise, use cellfun with a comparison function.
Step 2: Choose case-insensitive comparison
strcmpi ignores case, so cellfun(@strcmpi, C1, C2) applies it element-wise correctly.
Step 3: Why other options fail
cellfun(@strcmp, C1, C2) uses case-sensitive strcmp. strcmpi(C1{:}, C2{:}) unpacks cells into multiple arguments, causing a "too many input arguments" error. strcmp(C1, C2) is case-sensitive.
Final Answer:
cellfun(@strcmpi, C1, C2) -> Option A
Quick Check:
Use cellfun with strcmpi for cell array case-insensitive comparison [OK]
Hint: Use cellfun(@strcmpi, C1, C2) for cell array case-insensitive compare [OK]
Common Mistakes:
Using strcmp instead of strcmpi for ignoring case
Unpacking cell arrays with strcmpi(C1{:}, C2{:})
Not using cellfun for element-wise comparison
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.