How do I find the index of the first occurance of a string in a cell array?

52 ビュー (過去 30 日間)
lil brain
lil brain 2022 年 1 月 28 日
コメント済み: lil brain 2022 年 1 月 31 日
Hi,
this seems to be a bit harder than I originally thought. I am looking for occurances of events which are tracked by strings inside of my cell array. I am looking for a way where I can find and index the first occurances of the events/strings "Ball Glitch" and "Basket Glitch" in the second column person_1{2,:} of my cell array person_1 (see attachment).
In the cell array, next to each occurance of the strings, there is a column person_1{1,:} with all the run times for each event/string. I would like to sve the event/string and its corresponding rn time in a array.
I have tried using the 'strfnd' function but it gives me the error:
Error using strfind
Cell must be a cell array of character vectors.
Any tips on how to approach this issue?
  1 件のコメント
Image Analyst
Image Analyst 2022 年 1 月 28 日
You forgot the attachment. In the meantime, try ismember(), strfind(), strcmpi(), and/or contains(). These are your basic string processing functions.

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

採用された回答

Voss
Voss 2022 年 1 月 28 日
person_1 = { ...
1 99 0.04 pi/4 1e6; % run times
"Ball Glitch" "Some other event" "Basket Glitch" "Basket Glitch" "Ball Glitch"; ... % event names
}
person_1 = 2×5 cell array
{[ 1]} {[ 99]} {[ 0.0400]} {[ 0.7854]} {[ 1000000]} {["Ball Glitch"]} {["Some other event"]} {["Basket Glitch"]} {["Basket Glitch"]} {["Ball Glitch"]}
Note that person_1{1,:} is all cells in the first row of person_1 (not the first column). If you meant to say person{:,1}, then I would do:
person_1 = { ...
10 99 0.04 pi/4 1e6; % run times
"Ball Glitch" "Some other event" "Basket Glitch" "Basket Glitch" "Ball Glitch"; ... % event names
}.'
person_1 = 5×2 cell array
{[ 10]} {["Ball Glitch" ]} {[ 99]} {["Some other event"]} {[ 0.0400]} {["Basket Glitch" ]} {[ 0.7854]} {["Basket Glitch" ]} {[1000000]} {["Ball Glitch" ]}
Regardless, the following logic is the same whether its rows or columns, and that has nothing to do with the error you ran into, which is due to the second row/column of person_1 containing strings rather than character vectors (i.e., double-quoted things vs single-quoted things).
You can use strfind(); just convert the strings to character vectors first:
cellfun(@char,person_1(:,2),'UniformOutput',false)
ans = 5×1 cell array
{'Ball Glitch' } {'Some other event'} {'Basket Glitch' } {'Basket Glitch' } {'Ball Glitch' }
strfind(cellfun(@char,person_1(:,2),'UniformOutput',false),'Basket Glitch')
ans = 5×1 cell array
{0×0 double} {0×0 double} {[ 1]} {[ 1]} {0×0 double}
strfind(cellfun(@char,person_1(:,2),'UniformOutput',false),'Ball Glitch')
ans = 5×1 cell array
{[ 1]} {0×0 double} {0×0 double} {0×0 double} {[ 1]}
However, it's better to leave person_1(:,2) as strings and just use comparison for string equality (==):
[person_1{:,2}] == "Basket Glitch"
ans = 1×5 logical array
0 0 1 1 0
[person_1{:,2}] == "Ball Glitch"
ans = 1×5 logical array
1 0 0 0 1
So the final result you want, if I understand correctly, would be:
first_basket_glitch_info = person_1(find([person_1{:,2}] == "Basket Glitch",1),:)
first_basket_glitch_info = 1×2 cell array
{[0.0400]} {["Basket Glitch"]}
first_ball_glitch_info = person_1(find([person_1{:,2}] == "Ball Glitch",1),:)
first_ball_glitch_info = 1×2 cell array
{[10]} {["Ball Glitch"]}
  9 件のコメント
Voss
Voss 2022 年 1 月 30 日
No problem. Regarding #2, I guess using readcell() with the 'TextType','string' option would probably work, e.g.:
data_in = readcell([path_n, filename],'TextType','string');
but test it on one or more of your csv files to make sure.
lil brain
lil brain 2022 年 1 月 31 日
Great stuff! It worked. Appreciate the help with my project :)

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeData Type Identification についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by