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

57 vues (au cours des 30 derniers jours)
lil brain
lil brain le 28 Jan 2022
Commenté : lil brain le 31 Jan 2022
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 commentaire
Image Analyst
Image Analyst le 28 Jan 2022
You forgot the attachment. In the meantime, try ismember(), strfind(), strcmpi(), and/or contains(). These are your basic string processing functions.

Connectez-vous pour commenter.

Réponse acceptée

Voss
Voss le 28 Jan 2022
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 commentaires
Voss
Voss le 30 Jan 2022
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 le 31 Jan 2022
Great stuff! It worked. Appreciate the help with my project :)

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Data Type Identification dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by