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

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 Comment

You forgot the attachment. In the meantime, try ismember(), strfind(), strcmpi(), and/or contains(). These are your basic string processing functions.

Sign in to comment.

 Accepted Answer

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 Comments

Dear Benjamin,
this is exactly whats needed. Thank you! And thanks for the detailed explanation as well!
I have one more question if I may. I have cell array 19x1 (data set of all participants runtimes and events) with lists of runtimes and events in each cell (see attachment). I would ideally like to run your above code to find the first instances of "Basket Glitch" and "Ball Glitch" in each cell (each participant) and save those results in a new array called "all_runtimes".
The output would then ideally be one array with two columns. In the first column there would be all the runtimes for "Balls Glitch" and in the second column would be all the runtimes for "Basket Glitch". How would that look? Thanks!
This mat-file doesn't appear to have any event names (all cells are matrices of doubles):
load('runtimes_events.mat');
whos
Name Size Bytes Class Attributes ans 1x44 88 char runtime_event 19x1 11473640 cell
disp(runtime_event);
{23925×2 double} {21599×2 double} {55198×2 double} {50474×2 double} {28275×2 double} {29774×2 double} {23849×2 double} {19874×2 double} {37649×2 double} {36974×2 double} {87147×2 double} {31049×2 double} {19500×2 double} {20475×2 double} {79272×2 double} {30674×2 double} {22874×2 double} {45599×2 double} {52798×2 double}
runtime_event{1}
ans = 23925×2
23.2408 NaN 23.2541 NaN 23.2675 NaN 23.2808 NaN 23.2941 NaN 23.3075 NaN 23.3208 NaN 23.3342 NaN 23.3475 NaN 23.3608 NaN
But I can make up some data and show how that would work and then you can apply it to your data when you get it constructed.
runtime_event = { ...
{ ... % run times for person 1 | event names for person 1
23.2 "Ball Glitch"; ...
24.6 "Some other event"; ...
26.1 "Basket Glitch"; ...
28.7 "Basket Glitch"; ...
29.0 "Ball Glitch"; ...
}; ...
{ ... % run times for person 2 | event names for person 2
17.2 "Some other event"; ...
18.6 "Basket Glitch"; ...
19.1 "Ball Glitch"; ...
20.7 "Ball Glitch"; ...
23.0 "Basket Glitch"; ...
}; ...
{ ... % run times for person 2 | event names for person 2
19.2 "Some other event"; ...
22.6 "Basket Glitch"; ...
25.1 "Ball Glitch"; ...
28.7 "Ball Glitch"; ...
29.0 "Basket Glitch"; ...
}; ...
};
runtime_event
runtime_event = 3×1 cell array
{5×2 cell} {5×2 cell} {5×2 cell}
runtime_event{1}
ans = 5×2 cell array
{[23.2000]} {["Ball Glitch" ]} {[24.6000]} {["Some other event"]} {[26.1000]} {["Basket Glitch" ]} {[28.7000]} {["Basket Glitch" ]} {[ 29]} {["Ball Glitch" ]}
runtime_event{2}
ans = 5×2 cell array
{[17.2000]} {["Some other event"]} {[18.6000]} {["Basket Glitch" ]} {[19.1000]} {["Ball Glitch" ]} {[20.7000]} {["Ball Glitch" ]} {[ 23]} {["Basket Glitch" ]}
runtime_event{3}
ans = 5×2 cell array
{[19.2000]} {["Some other event"]} {[22.6000]} {["Basket Glitch" ]} {[25.1000]} {["Ball Glitch" ]} {[28.7000]} {["Ball Glitch" ]} {[ 29]} {["Basket Glitch" ]}
Now grab the runtime corresponding to the first "Ball Glitch" event for each person and the runtime corresponding to the first "Basket Glitch" event for each person, and put them together in a matrix:
all_runtimes = [ ...
cellfun(@(x)x{find([x{:,2}] == "Ball Glitch",1),1},runtime_event) ...
cellfun(@(x)x{find([x{:,2}] == "Basket Glitch",1),1},runtime_event) ...
];
disp(all_runtimes);
23.2000 26.1000 19.1000 18.6000 25.1000 22.6000
Dear Benjamin,
it seems as though I dont quite understand your last piece of code. Does it mean that I need to input the runtimes for each event into the code? Because if I run it as shown by you I get the error:
Brace indexing is not supported for variables of this type.
Error in @(x)x{find([x{:,2}]=="Ball Glitch",1),1}
Sorry if this is obvious I am still a novice :)
Yeah, so your data was a cell array of matrices of doubles. Event names are strings or char vectors (not doubles) so I knew there was something wrong with that data - no event names.
So I made up some data that was a cell array of cell arrays, with each cell array being 2D with first column being times (doubles) and second column being event names (strings).
This is all shown in my last comment.
So the code I wrote was for the data I made up (which has the same structure and type you said your data was, but not the same as the data that was actually posted). If you run that code with the mat file you posted, it won't work because the cells contain matrices of doubles (with NaNs in the second column) rather than cell arrays with event names in the second column.
You don't necessarily need to input anything into the code, but you should figure out: 1) why that mat file is not of the structure it should be, and 2) how to generate or get one that has the structure you described, which is what my code is based on.
Hi Benjamin, thank you for the explanation. I have been trying to solve the fact that the strings were shown as NaNs instead of strings and I think I found the answer. It was because I used the 'readmatrix' fucntion instead of 'readcell' in this code:
% opens UI to select the csv files which will be imported
[file_list, path_n] = uigetfile('.csv', 'Grab csv', 'Multiselect', 'on');
if ~iscell(file_list)
file_list = {file_list};
end
runtime_event = cell(length(file_list),1);
for i = 1:length(file_list)
filename = file_list{i};
data_in = readcell([path_n, filename]);
runtime_event{i} = data_in(:,3:4);
end
However if I now use your code
all_runtimes = [cellfun(@(x)x{find([x{:,2}] == "Balls Glitch ",1),1},runtime_event)]
on my runtime_event cell array I still receive the same error. What am I not understanding?
Thanks for your help!
Well, there are three things going on here:
1) some of those cells are empty (see the 0x0 double below); my code wouldn't work on that since it assumes they're all two-column cell arrays.
2) the second column of the non-empty cells contain character vectors rather than strings, which are a different data type and for which comparison and concatenation work differently. the code I wrote was for strings, like the original question was asking about.
3) the code I wrote matches 'Ball Glitch'; this data set says 'Balls Glitch' (note the s).
Below is the code I wrote just now to figure out those 3 things. The last line contains the command you can use on this data set to find the runtimes of first match of 'Balls Glitch', but you'll want to be sure to run this line first to remove those 0x0 doubles:
runtime_event(cellfun(@isempty,runtime_event)) = [];
You can also refer to the code below to debug your own code in the future. In general, try to make sure to import the data the same way each time and use the same data types and words, or else you have to write more general code that can handle all possible situations.
load('runtime_event.mat');
whos
Name Size Bytes Class Attributes ans 1x42 84 char runtime_event 19x1 96073910 cell
disp(runtime_event);
{23925×2 cell } {21599×2 cell } {55198×2 cell } {50474×2 cell } {28275×2 cell } {29774×2 cell } {23849×2 cell } {19874×2 cell } {37649×2 cell } { 0×0 double} { 0×0 double} { 0×0 double} { 0×0 double} { 0×0 double} { 0×0 double} { 0×0 double} { 0×0 double} { 0×0 double} { 0×0 double}
runtime_event{1}
ans = 23925×2 cell array
{[23.2408]} {'Game Started'} {[23.2541]} {'Game Started'} {[23.2675]} {'Game Started'} {[23.2808]} {'Game Started'} {[23.2941]} {'Game Started'} {[23.3075]} {'Game Started'} {[23.3208]} {'Game Started'} {[23.3342]} {'Game Started'} {[23.3475]} {'Game Started'} {[23.3608]} {'Game Started'} {[23.3742]} {'Game Started'} {[23.3875]} {'Game Started'} {[23.4008]} {'Game Started'} {[23.4141]} {'Game Started'} {[23.4275]} {'Game Started'} {[23.4408]} {'Game Started'}
runtime_event(cellfun(@isempty,runtime_event)) = [];
disp(runtime_event);
{23925×2 cell} {21599×2 cell} {55198×2 cell} {50474×2 cell} {28275×2 cell} {29774×2 cell} {23849×2 cell} {19874×2 cell} {37649×2 cell}
names = runtime_event{1}(:,2)
names = 23925×1 cell array
{'Game Started'} {'Game Started'} {'Game Started'} {'Game Started'} {'Game Started'} {'Game Started'} {'Game Started'} {'Game Started'} {'Game Started'} {'Game Started'} {'Game Started'} {'Game Started'} {'Game Started'} {'Game Started'} {'Game Started'} {'Game Started'} {'Game Started'} {'Game Started'} {'Game Started'} {'Game Started'} {'Game Started'} {'Game Started'} {'Game Started'} {'Game Started'} {'Game Started'} {'Game Started'} {'Game Started'} {'Game Started'} {'Game Started'} {'Game Started'}
idx = find(ismember(names,{'Balls Glitch ','Ball Glitch','Ball Glitch ','Balls Glitch'}));
names(idx)
ans = 530×1 cell array
{'Balls Glitch'} {'Balls Glitch'} {'Balls Glitch'} {'Balls Glitch'} {'Balls Glitch'} {'Balls Glitch'} {'Balls Glitch'} {'Balls Glitch'} {'Balls Glitch'} {'Balls Glitch'} {'Balls Glitch'} {'Balls Glitch'} {'Balls Glitch'} {'Balls Glitch'} {'Balls Glitch'} {'Balls Glitch'} {'Balls Glitch'} {'Balls Glitch'} {'Balls Glitch'} {'Balls Glitch'} {'Balls Glitch'} {'Balls Glitch'} {'Balls Glitch'} {'Balls Glitch'} {'Balls Glitch'} {'Balls Glitch'} {'Balls Glitch'} {'Balls Glitch'} {'Balls Glitch'} {'Balls Glitch'}
% all_runtimes = cellfun(@(x)x{find([x{:,2}] == "Balls Glitch",1),1},runtime_event)
all_runtimes = cellfun(@(x)x{find(strcmp(x(:,2),'Balls Glitch'),1),1},runtime_event)
all_runtimes = 9×1
206.6739 248.8354 813.5485 377.6799 216.1009 242.1685 224.4612 192.4467 411.5744
Dear Benjamin,
thanks for the reply. Regarding your points
1) Yes correct, I have accidentally stopped running the script early leading to empty cells. I have managed to get the full array now and your code works!
2) I see, was this the case because I opened the files using "readcell" instead of "readmatrix"? What function would I need to use/ how would I need to read the data in order to create a column of events as strings rather than character vectors?
3) Yes, thanks for pointing that out. I already changed it to Balls Glitch.
I am learning a lot here and thanks to the debugging code!
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.
Great stuff! It worked. Appreciate the help with my project :)

Sign in to comment.

More Answers (0)

Categories

Asked:

on 28 Jan 2022

Commented:

on 31 Jan 2022

Community Treasure Hunt

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

Start Hunting!