Bird
Raised Fist0
MATLABdata~10 mins

String comparison in MATLAB - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - String comparison
Start
↓
Input two strings
↓
Compare strings
↓
Are strings equal?
Yes→Output: true
↓
Output: false
↓
End
The program takes two strings, compares them, and outputs true if they are the same, otherwise false.
Execution Sample
MATLAB
str1 = 'hello';
str2 = 'Hello';
result = strcmp(str1, str2);
This code compares two strings 'hello' and 'Hello' and stores true or false in result.
Execution Table
StepActionEvaluationResult
1Assign str1str1 = 'hello'str1 = 'hello'
2Assign str2str2 = 'Hello'str2 = 'Hello'
3Compare str1 and str2 using strcmpstrcmp('hello', 'Hello')0
4Store resultresult = 0result = 0
💡 Comparison returns false because 'hello' and 'Hello' differ in case.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
str1undefined'hello''hello''hello''hello'
str2undefinedundefined'Hello''Hello''Hello'
resultundefinedundefinedundefined00
Key Moments - 2 Insights
Why does strcmp return false even though the words look similar?
Because strcmp is case-sensitive, it treats 'hello' and 'Hello' as different strings (see execution_table step 3).
What if we want to compare strings ignoring case?
Use strcmpi instead of strcmp to ignore case differences (not shown here but important to know).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, what is the result of strcmp('hello', 'Hello')?
Aerror
Btrue
Cfalse
Dundefined
💡 Hint
Check the 'Result' column in execution_table row for step 3.
At which step is the variable 'result' assigned a value?
AStep 4
BStep 2
CStep 1
DStep 3
💡 Hint
Look at the 'Action' column for when 'result' is stored.
If str2 was changed to 'hello' (all lowercase), what would strcmp return at step 3?
Afalse
Btrue
Cerror
Dundefined
💡 Hint
strcmp returns true only if strings are exactly the same (see variable_tracker for str1 and str2 values).
Concept Snapshot
String comparison in MATLAB:
- Use strcmp(str1, str2) to check if two strings are exactly equal.
- strcmp is case-sensitive.
- Returns logical true (1) if equal, false (0) if not.
- Use strcmpi to ignore case differences.
- Result can be stored in a variable for further use.
Full Transcript
This example shows how MATLAB compares two strings using strcmp. First, two strings are assigned to variables str1 and str2. Then strcmp compares them character by character, considering case. Since 'hello' and 'Hello' differ in case, strcmp returns false. The result is stored in the variable result. Beginners often confuse case sensitivity, so remember strcmp treats uppercase and lowercase letters as different. To ignore case, use strcmpi. This trace helps visualize each step and variable change during string comparison.

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

  1. Step 1: Understand string comparison functions

    MATLAB provides strcmp for case-sensitive string comparison and strcmpi for case-insensitive comparison.
  2. Step 2: Identify the correct function for case-sensitive comparison

    strcmp compares strings exactly, including letter case, so it is the correct choice.
  3. Final Answer:

    strcmp -> Option B
  4. 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

  1. Step 1: Recall syntax for case-insensitive comparison

    strcmpi compares two strings ignoring case and takes two string inputs as separate arguments.
  2. 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.
  3. Final Answer:

    strcmpi(s1, s2) -> Option C
  4. 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?
s1 = "Hello";
s2 = "hello";
result = strcmp(s1, s2);
disp(result);
medium
A. "true"
B. 1
C. Error
D. 0

Solution

  1. Step 1: Understand strcmp behavior

    strcmp compares strings with case sensitivity. "Hello" and "hello" differ in case.
  2. Step 2: Evaluate strcmp result

    Since the strings differ in case, strcmp returns logical 0 (false), which disp prints as 0.
  3. Final Answer:

    0 -> Option D
  4. 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

  1. Step 1: Analyze the use of '==' inside strcmpi

    The expression s1 == s2 compares strings element-wise, returning a logical array, not a string.
  2. Step 2: Understand strcmpi input requirements

    strcmpi expects two string inputs, not a logical array. Using '==' inside causes a syntax or runtime error.
  3. Final Answer:

    Using '==' inside strcmpi causes an error -> Option A
  4. 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

  1. Step 1: Understand cell array string comparison

    To compare each pair of strings in cell arrays element-wise, use cellfun with a comparison function.
  2. Step 2: Choose case-insensitive comparison

    strcmpi ignores case, so cellfun(@strcmpi, C1, C2) applies it element-wise correctly.
  3. 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.
  4. Final Answer:

    cellfun(@strcmpi, C1, C2) -> Option A
  5. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard