MATLAB: How to use for loop on group of rows in table

for loopMATLAB

Hi all,
I have a dataset (excel file) in which all the data of all participants (n = 23) is stored together. Each row contains data of a separate trial, resulting in 70 rows per participant. My participants have different names (i.e. numbers, from 1-23). Here is a simplified example of how the table looks:
Subject Stimulus Running TestStim_ACC TestStim_CRESP TestStim_RESP
_______ ________ _______ ____________ ______________ _____________
1 'A' 'List1' 1 1 1
1 'B' 'List1' 0 2 2
1 'C' 'List1' 1 2 1
1 'D' 'List1' 1 3 3
2 'A' 'List1' 0 3 3
2 'B' 'List1' 1 3 3
2 'C' 'List1' 0 2 2
2 'D' 'List1' 0 3 2
3 'A' 'List1' 0 2 2
3 'B' 'List1' 1 2 2
3 'C' 'List1' 0 1 3
3 'D' 'List1' 0 2 3
4 'A' 'List1' 1 3 3
4 'B' 'List1' 1 2 2
4 'C' 'List1' 0 1 1
4 'D' 'List1' 1 2 2
Now I would like to create a for loop on this data per participant. The for loop works, except for the 'for each separate participant'-part. When I now run the for loop, it loops through the complete dataset (all the 1610 rows) and takes all the data together. This is what the loop needs to do:
for i = 1:length(dataTestStim.Subject)
F = contains(dataTestStim.Stimulus,'Fea&')...
& dataTestStim.TestStim_ACC == 1
SumF = sum(F);
A = contains(dataTestStim.Stimulus,'Ang&')...
& dataTestStim.TestStim_ACC == 1
SumA = sum(A);
H = contains(dataTestStim.Stimulus,'Hap&')...
& dataTestStim.TestStim_ACC == 1
SumH = sum(H);
Sa = contains(dataTestStim.Stimulus,'Sad&')...
& dataTestStim.TestStim_ACC == 1
SumSa = sum(Sa)
Su = contains(dataTestStim.Stimulus,'Sur&')...
& dataTestStim.TestStim_ACC == 1
SumSu = sum(Su);
D = contains(dataTestStim.Stimulus,'Dis&')...
& dataTestStim.TestStim_ACC == 1
SumD = sum(D);
end
Not surprisingly, the length of dataTestStim.Subject is 1610, so I guess there's my problem. Based on what I read on the internet I've tried using the
unique function
or the
findgroups function
Unfortunately I'm relatively new to Matlab and I don't seem to be able to make it work. How could I get all the results of the for loop for all 23 participants separately (also without overwriting them)? It would also be great if I could store/save the results.
Thank you!

Best Answer

Stephanie, you may have a reason dfor wanting to split up your data, but it's very often not necessary. It looks like maybe what you are doing is computing sums for each participant, and that's easy to do using findgroups/splitapply, groupsummary (in newer versions) or varfun. For example
>> t = readtable('tmp.dat')
t =
16×6 table
Subject Stimulus Running TestStim_ACC TestStim_CRESP TestStim_RESP
_______ ________ _______ ____________ ______________ _____________
1 'A' 'List1' 1 1 1
1 'B' 'List1' 0 2 2
1 'C' 'List1' 1 2 1
1 'D' 'List1' 1 3 3
2 'A' 'List1' 0 3 3
2 'B' 'List1' 1 3 3
2 'C' 'List1' 0 2 2
2 'D' 'List1' 0 3 2
3 'A' 'List1' 0 2 2
3 'B' 'List1' 1 2 2
3 'C' 'List1' 0 1 3
3 'D' 'List1' 0 2 3
4 'A' 'List1' 1 3 3
4 'B' 'List1' 1 2 2
4 'C' 'List1' 0 1 1
4 'D' 'List1' 1 2 2
>> varfun(@sum,t,'InputVariables',4:6,'GroupingVariables','Subject')
ans =
4×5 table
Subject GroupCount sum_TestStim_ACC sum_TestStim_CRESP sum_TestStim_RESP
_______ __________ ________________ __________________ _________________
1 4 3 8 7
2 4 1 11 10
3 4 1 7 10
4 4 3 8 8