Why use strcmp instead of = ? [MATLAB]

  • MATLAB
  • Thread starter nickadams
  • Start date
  • Tags
    Matlab
In summary, strcmp is preferred over the = operator in MATLAB because it allows for more flexibility and accuracy when comparing strings. Unlike the = operator, strcmp takes into account the case and whitespace of strings, making it a more robust method for string comparison. Additionally, strcmp can be used to compare multiple strings at once, whereas the = operator can only compare two strings at a time. Therefore, using strcmp can lead to more accurate and efficient code in MATLAB.
  • #1
nickadams
182
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
  • #2
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.
 
  • #3
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.
 
  • #4
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:
  • #5


strcmp is a function in MATLAB that allows for more flexibility and accuracy when comparing strings. When using the = operator, MATLAB will only return true if the two strings are exactly the same, including case sensitivity. However, with strcmp, you can specify the comparison to be case insensitive and also allow for alternative forms of a string to be considered as a match. This means that in your case, the user could input "yes", "Yes", "y", or "Y" and strcmp will still return true, while the = operator would only return true if the user inputs "y". Additionally, strcmp also allows for more complex comparisons, such as comparing strings with different lengths or special characters. In summary, using strcmp instead of = provides more robust and accurate string comparisons in MATLAB.
 

Related to Why use strcmp instead of = ? [MATLAB]

What is the purpose of using strcmp instead of the = operator in MATLAB?

The strcmp function in MATLAB is specifically designed for string comparisons, while the = operator is meant for assigning values to variables. Using strcmp ensures that the comparison is done correctly and consistently for strings, regardless of their length or content.

How does strcmp differ from the = operator in terms of functionality?

The = operator in MATLAB compares the values of two variables and returns a logical 1 if they are equal, or 0 if they are not. On the other hand, strcmp compares the characters in two strings and returns a logical 1 if they are identical, or 0 if they are not. This makes strcmp more suitable for string comparisons.

Can the = operator be used for string comparisons in MATLAB?

While the = operator can technically be used for string comparisons in MATLAB, it is not recommended. This is because the = operator performs a case-sensitive comparison, which may not be desired in all cases. For more accurate and consistent string comparisons, strcmp should be used instead.

Are there any limitations to using strcmp over the = operator in MATLAB?

One limitation of strcmp is that it only works for comparing strings. It cannot be used for numeric or other data types. Additionally, strcmp only compares two strings at a time, while the = operator can be used for multiple variable assignments at once.

In what situations is it necessary to use strcmp instead of the = operator in MATLAB?

It is necessary to use strcmp when performing string comparisons in MATLAB, such as checking for the equality of two strings or sorting strings in alphabetical order. Additionally, strcmp should be used when the case of the strings needs to be ignored, or when comparing strings of different lengths.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
597
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
9
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
5K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
10
Views
2K
Back
Top