Is there unknown error in strcmp?

1 Ansicht (letzte 30 Tage)
Casey
Casey am 1 Okt. 2012
Hi,
I tried to use strcmp between a string and a cell array. Instead of getting '1', I keep getting '0'. May I know where I go wrong?
Example:
D=[[28];[28];[28]...[0];[0]]
Command Window
>>strcmp('[28]',D(2));
ans = 0

Akzeptierte Antwort

Andrei Bobrov
Andrei Bobrov am 1 Okt. 2012
Bearbeitet: Andrei Bobrov am 1 Okt. 2012
D={[28];[28];[28];[0];[0]};
strcmp('[28]',['[',num2str(D{2}),']'])
or
isequal({28},D(2))
or
28 == D{2}
or
abs(28 - D{2}) < eps(100)
  3 Kommentare
Jan
Jan am 1 Okt. 2012
The 1st solution is really strange. Please note that: D = {[28]} is equivalent to D={28}, which means that you store a scalar double with value 28. Surrounding a scalar by square brackets wastes time and reduses the readability only. STRCMP() operates on strings, which are CHAR vectors.
Finally I suggest to use the 3rd method D{2} == 28, because converting the value to a string is very indirekt and inefficient.
Casey
Casey am 1 Okt. 2012
Okay, thanks alot!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Characters and Strings finden Sie in Help Center und File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by