Why does strcmp take numerical arguments?
Afficher commentaires plus anciens
I found an old bug in my code. I pass by mistake a numerical value to strcmp. Now, I made a little test with R2013a
num = double('A');
is1 = strcmp( num, {'A','B','C'} );
is2 = strcmp( num, {'A','B','C',num} );
is3 = strcmp( num, {'A','B','C',num2str(num)} );
[any(is1),any(is2),any(is3)]
returns
ans =
0 0 0
strcmp accepts the numerical argument, but doesn't find anything. I would have appreciated an error together with a message.
On the other hand, I use a similar behavior of strfind. It takes numerical inputs.
n1 = (65:67);
n2 = (61:70);
strfind( n2, n1 ) % us' trick
returns
ans =
5
That is an efficient way to search for sub-sequences in row vectors of flints (whole numbers). Last millennium the difference in speed mattered.
Réponse acceptée
Plus de réponses (1)
Another example:
strcmp(1, 1) % FALSE
strcmp(pi, pi) % FALSE
This behavior can be observed in 2011b, 2009a and version 6.5 also. Therefore it looks like a backward compatibility feature.
Catégories
En savoir plus sur Cell Arrays dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!