Why use strcmp instead of = ? [MATLAB]

  • Context: MATLAB 
  • Thread starter Thread starter nickadams
  • Start date Start date
  • Tags Tags
    Matlab
Click For Summary

Discussion Overview

The discussion revolves around the use of the strcmp function in MATLAB for string comparison, particularly in the context of conditional statements. Participants explore the implications of using relational operators versus strcmp, and the differences in string comparison behavior in MATLAB compared to other programming languages like C++ and Java.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant suggests using strcmp for string comparison in MATLAB, noting that it allows for comparison of vectors of different lengths and may stop comparing at the first difference.
  • Another participant emphasizes that using == in other languages like C++ and Java can lead to confusion, as it checks for memory reference equality rather than content equality.
  • A later reply challenges the accuracy of the previous explanation regarding C++, stating that C++'s std::string type performs lexicographical comparisons and that the addition of string literals is not valid in C++.
  • There is a mention that Java does not have a strcmp function, and string comparison is done using the .equals method instead.

Areas of Agreement / Disagreement

Participants express differing views on the behavior of string comparison in C++ and Java, leading to unresolved disagreements regarding the correctness of the claims made about these languages.

Contextual Notes

Some assumptions about string behavior in different programming languages are not fully explored, and there are unresolved points regarding the specific implementations of string types and comparison methods.

nickadams
Messages
182
Reaction score
0
If a user answers "yes", "Yes", "y", or "Y" to an input question, I want to enter the while loop. But when I tried to use


user=input('blahblahblah [y/n]: ','s');

while user=='y' || user='Y' || user=='yes' || user=='Yes'


MATLAB recommends I change to strcmp. Why is this?
 
Physics news on Phys.org
To test if two strings are equivalent, use strcmp, which allows vectors of dissimilar length to be compared.

http://www.mathworks.com/help/techdoc/ref/relationaloperators.html

Even if the strings had the same length, using == might result in the creation of an intermediate logical array of the same size, whereas strcmp might stop comparing as soon as the first difference was encountered.
 
It's also a very good habit to learn if you ever move to C++ or Java, because in those languages == and strcmp() do fundamentally different things.

In C++, == checks if the two strings share the same memory, not if the contents of the memory are equal.

So for code like
String a = "string" + " one";
String b = "string one";
String c = a;

You get the strange result that a == c is true (because a and c are both references to the same String object), but a == b is false (because a and b are different String objects).

But strcmp(a,b) and strcmp(a,c) are both true, which is probably how you wanted the comparison to behave.
 
AlephZero said:
It's also a very good habit to learn if you ever move to C++ or Java, because in those languages == and strcmp() do fundamentally different things.

In C++, == checks if the two strings share the same memory, not if the contents of the memory are equal.

So for code like
String a = "string" + " one";
String b = "string one";
String c = a;

You get the strange result that a == c is true (because a and c are both references to the same String object), but a == b is false (because a and b are different String objects).

But strcmp(a,b) and strcmp(a,c) are both true, which is probably how you wanted the comparison to behave.

This is not correct. C++ has no String type. For, C++'s std::string type, http://msdn.microsoft.com/en-us/library/8ww0haah.aspx" does perform a lexicographical comparison, so a == b would be true. Also, you may not add two c-string literals in C++.


Java, which does have a String type, does not have an strcmp function at all. This functionality may be performed with the .equals method.
 
Last edited by a moderator:

Similar threads

  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 5 ·
Replies
5
Views
4K