Is there unknown error in strcmp?

3 ビュー (過去 30 日間)
Casey
Casey 2012 年 10 月 1 日
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.
Casey
Casey 2012 年 10 月 1 日
Okay, thanks alot!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by