Effacer les filtres
Effacer les filtres

Is there unknown error in strcmp?

2 vues (au cours des 30 derniers jours)
Casey
Casey le 1 Oct 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

Réponse acceptée

Andrei Bobrov
Andrei Bobrov le 1 Oct 2012
Modifié(e) : Andrei Bobrov le 1 Oct 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 commentaires
Jan
Jan le 1 Oct 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 le 1 Oct 2012
Okay, thanks alot!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Characters and Strings dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by