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)
Afficher commentaires plus anciens
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
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.
Réponse acceptée
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
}
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
}.'
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)
strfind(cellfun(@char,person_1(:,2),'UniformOutput',false),'Basket Glitch')
strfind(cellfun(@char,person_1(:,2),'UniformOutput',false),'Ball Glitch')
However, it's better to leave person_1(:,2) as strings and just use comparison for string equality (==):
[person_1{:,2}] == "Basket Glitch"
[person_1{:,2}] == "Ball Glitch"
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_ball_glitch_info = person_1(find([person_1{:,2}] == "Ball Glitch",1),:)
9 commentaires
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.
Plus de réponses (0)
Voir également
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!