フィルターのクリア

Info

この質問は閉じられています。 編集または回答するには再度開いてください。

Comparing two equal strings doesnt work

1 回表示 (過去 30 日間)
Christian Schumacher
Christian Schumacher 2019 年 10 月 19 日
閉鎖済み: MATLAB Answer Bot 2021 年 8 月 20 日
I have a list of strings (containing hockey player names) and I want to find the index for a specific name.I never had such an easy problem before and it is driving my insane.
I tried this:
player and index are both "Connor McDavid"
player = Player(1);
index1 = string(200);
logic = strcmp(index1 ,player);
Here are screenshots too ensure that they contain the same value.
Unbenannt.PNG
Unbenannt.PNG
logic should return a 1 but it returns 0. I tried == too but it doesnt help.
Pls help me, i dont know what to do anymore.
  9 件のコメント
Daniel M
Daniel M 2019 年 10 月 20 日
編集済み: Daniel M 2019 年 10 月 20 日
Haha, oh yeah, they are strings.
char(player)-'A'
Walter Roberson
Walter Roberson 2019 年 10 月 20 日
160 is U+00A0, "non-breaking space"
player = replace(player, char(160), ' ');
index1 = replace(index1, char(160), ' ');
but it would probably make more sense to replace all of the 160 in your data source (perhaps immediately after retrieving it) instead of doing the replacement each iteration.

回答 (1 件)

Prasad Parameswaran
Prasad Parameswaran 2019 年 10 月 22 日
編集済み: Prasad Parameswaran 2019 年 10 月 22 日
This can be solved by using the "ismember" function, refer the below example:
A = ["John"; "Lee" ; "James"; "Lee"];
B = "Lee";
index = ismember(A,B)
The output is
index =
4×1 logical array
0
1
0
1

この質問は閉じられています。

Community Treasure Hunt

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

Start Hunting!

Translated by