Is there unknown error in strcmp?

2 次查看(过去 30 天)
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

采纳的回答

Andrei Bobrov
Andrei Bobrov 2012-10-1
编辑:Andrei Bobrov 2012-10-1
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 个评论
Jan
Jan 2012-10-1
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.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Characters and Strings 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by