MATLAB: Get average of all rows in table that match two exact strings

averagestringtable

I have a table that is formatted like this:
sub type window mean stdev rms
_____ ____ ______ _______ ______ ______
'S03' 'e4' 'w1' -0.6357 0.3274 0.715
'S03' 'e4' 'w1' -0.591 0.3106 0.6676
'S03' 'e4' 'w2' -0.4616 0.3607 0.5857
'S03' 'e4' 'w2' -0.457 0.3387 0.5687
'S03' 'e4' 'w2' -0.5737 0.2689 0.6335
'S03' 'e4' 'w3' -0.4818 0.2139 0.5271
'S03' 'e4' 'w3' -0.4272 0.1523 0.4536
'S06' 'e4' 'w1' -0.6357 0.3274 0.715
'S06' 'e4' 'w2' -0.591 0.3106 0.6676
'S06' 'e4' 'w2' -0.4616 0.3607 0.5857
'S06' 'e4' 'w2' -0.457 0.3387 0.5687
'S06' 'e4' 'w2' -0.5737 0.2689 0.6335
'S06' 'e4' 'w3' -0.4818 0.2139 0.5271
For each subject (e.g. S03 & S06 shown above) I need to get the average mean, stdev, and rms for each window (3 windows shown above: w1-w3).
So I need to check the subject string (1st col) and the window string (3rd col) to check if the rows belong to the same subejct and window, and then calculate the average values for the mean, stdev, and rms columns.
The data is attached as a .mat file.
I've tried the following but cant get it to work:
rowssub = any(strcmp(accStats,'S03'),1);
rowsw = any(strcmp(accStats,'w1'),1);
The result ends up being a 1×1 logical with value of 0.

Best Answer

The easiest way:
varfun(@mean, yourtable, 'GroupingVariables', {'sub', 'window'}, 'InputVariables', {'mean', 'stdev', 'rms'})