Azzera filtri
Azzera filtri

Is there unknown error in strcmp?

2 visualizzazioni (ultimi 30 giorni)
Casey
Casey il 1 Ott 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

Risposta accettata

Andrei Bobrov
Andrei Bobrov il 1 Ott 2012
Modificato: Andrei Bobrov il 1 Ott 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 Commenti
Jan
Jan il 1 Ott 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 il 1 Ott 2012
Okay, thanks alot!

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Characters and Strings in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by