Curious case of identical strings

11 ビュー (過去 30 日間)
Nikola Mirkov
Nikola Mirkov 2015 年 9 月 8 日
コメント済み: Star Strider 2015 年 9 月 9 日
Hi everybody,
Here is the code I run:
findcellstr('UKCBRSRBR',namesshort)
findcellstr('UKCBRSRBR',namesshort)
where the function 'findcellstr' looks for a string in a cell array using the 'for' loop with 'strmatch' function. Albeit identical, the first string I'm looking for is copy-pasted from an Excel file and the second typed-in by hand.
The output I get is:
ans = []
ans = 47
Let me now use the Matlab functions only and run the following code:
find(cellfun(@isempty,strfind(namesshort,'UKCBRSRBR'))==0)
find(cellfun(@isempty,strfind(namesshort,'UKCBRSRBR'))==0)
I still get:
ans = Empty matrix: 0-by-1
ans = 47
Why?
Thank you so much for your reply,
niko
  2 件のコメント
Stephen23
Stephen23 2015 年 9 月 8 日
編集済み: Stephen23 2015 年 9 月 8 日
Please upload the complete code that is relevant to the problem, in particular the code for findcellstr, and show give use an example value for namesshort.
The obvious answer is that the strings are not the same. To view the actual character that make up the strings try this: double(str), you could even assign them to variables and find where they differ.
Also note that according to the documentation strmatch is not recommended, you should use strncmp instead.
Nikola Mirkov
Nikola Mirkov 2015 年 9 月 9 日
Thank you Stephen!

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

回答 (2 件)

Star Strider
Star Strider 2015 年 9 月 8 日
That you ‘...copy-pasted from an Excel file...’ may be the problem.
I’m not certain how Excel data are represented internally, but perhaps a better option would be to use xlsread to import it into your MATLAB workspace and then do the comparison.
  2 件のコメント
Nikola Mirkov
Nikola Mirkov 2015 年 9 月 9 日
Thank you for your effort, Star Strider.
Star Strider
Star Strider 2015 年 9 月 9 日
My pleasure.

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


Nikola Mirkov
Nikola Mirkov 2015 年 9 月 9 日
Dear Stephen,
Your answer was very helpful - the strings are indeed different when I convert them to a vector using double(). So how would you then go about converting that vector of numbers back into a string?
Thank you so much for your time!
niko
  1 件のコメント
Nikola Mirkov
Nikola Mirkov 2015 年 9 月 9 日
Found it, I can use char(). Thanks!

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

カテゴリ

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