Mean, Max and Min of Structured Cell array

Hi all, I have a structured cell array where I need to compute the mean of each cell from column 4. Column 3 contains the name of the data (repeated for number of days 31, 29 etc.). At the end I would like to display the results as name (3rd column one value) , Average, Min and Max of each cell of 4th column data. Any help (sample example) in this regard is highly appreciated. For your reference I have attached the picture.

 Accepted Answer

You should first convert everything to table form.
Column3={{'dog';'dog';'dog'},{'cat';'cat'}}; %Original data
Column4={[1;2;3], [4;5]};
Column3=string( vertcat(Column3{:}) );
Column4=vertcat( Column4{:} );
T=table(Column3,Column4,'VariableNames',{'Names','Data'})
T = 5×2 table
Names Data _____ ____ "dog" 1 "dog" 2 "dog" 3 "cat" 4 "cat" 5
From that point on, it all becomes much easier:
Means = varfun(@mean,T,'GroupingVariables','Names')
Means = 2×3 table
Names GroupCount mean_Data _____ __________ _________ "cat" 2 4.5 "dog" 3 2
Maxs= varfun(@max,T,'GroupingVariables','Names')
Maxs = 2×3 table
Names GroupCount max_Data _____ __________ ________ "cat" 2 5 "dog" 3 3

14 Comments

Hi Matt, thanks for sample answer. Suppose Tc is the actual data could you please let me know how to access column 3 and 4 information (structured cell data from my example). I will go through your code in detail and try to work on it on my data. Thanks again
Column3=Tc(:,3);
Column4=Tc(:,4);
Hi Matt, thanks again. In Column 3 the variable "dog' is repeated 31 times etc. whereas in column 4, each cell has 31 or 29 data in it. In order to mimic your code do I need to use loop to extract the mean of each cell (mean from 31 samples from each cell)? Please let me know. But at least your example has given me some directions to work on this data. I will accept this answer once I achieve my result. Thanks and regards.
Matt J
Matt J on 8 Apr 2021
Edited: Matt J on 8 Apr 2021
My example should work for you without modification.
Hi Matt I tried to use your method but it is not working for me. For my data, column 3 contains the same variable information "subject_ID1" for each cell (Subject_ID1 for 31 days) so I need to extract just one value for each cell in Column 3. For Column 4 I need to compute mean for each cell. For your reference I have attached a sample data.
Your Sample.mat is not a 4-column cell array like you originally posted. Regardless, once Column3 and Column4 are appropriately assigned, I'm seeing no problem with the output,
load(websave('Sample.mat', 'https://www.mathworks.com/matlabcentral/answers/uploaded_files/577972/Sample.mat'))
Column3=Sample.Subjects;
Column4=Sample.AHI;
Column3=string( vertcat(Column3{:}) );
Column4=vertcat( Column4{:} );
T=table(Column3,Column4,'VariableNames',{'Names','Data'});
Means = varfun(@mean,T,'GroupingVariables','Names')
Means = 5×3 table
Names GroupCount mean_Data __________________ __________ _________ "001352tP2lnwGigq" 31 0.8345 "004fvTNq5S1rwmdy" 31 0.96764 "008LAKhG4nFZbD1h" 31 0.80035 "00X6LElMhyxcDLO3" 31 0.96794 "00cbPNUf6bSxKaiw" 31 0.93715
Hi Matt, thank you so much for the answer and help. Column 1 and Column 2 contains ID and Day information and for my anlysis it is not that much important. But now (with your code) I can modify it according to my requirement. I am going to accept this answer!!!
Thanks again.
Hi Matt, thanks for your help. Now, i am tryting to extract the one day (day1) information from the data (sample data attached) using "varfun" where I tried to create an indexing function using "day1=@(x) find(x==1);" but I am not able to get the indexed data. I am able to extract mean, min, max etc using inbuilt function but fail to extract the indexed data. Could you please guide me.
Matt J
Matt J on 10 Apr 2021
Edited: Matt J on 10 Apr 2021
Shouldn't it be @(x) x(1) ?
Hi Matt I tried that as well, unfortunately that didnot work for me.
It works fine on our original toy example:
Column3={{'dog';'dog';'dog'},{'cat';'cat'}}; %Original data
Column4={[1;2;3], [4;5]};
Column3=string( vertcat(Column3{:}) );
Column4=vertcat( Column4{:} );
T=table(Column3,Column4,'VariableNames',{'Names','Data'})
T = 5×2 table
Names Data _____ ____ "dog" 1 "dog" 2 "dog" 3 "cat" 4 "cat" 5
Firsts = varfun(@(x) x(1),T,'GroupingVariables','Names')
Means = 2×3 table
Names GroupCount Fun_Data _____ __________ ________ "cat" 2 4 "dog" 3 1
Hi Matt, yes hence, I followed the same and other variations. Now, I need to extract the information from each subject for day 1 only. Hence, I tried to use find function to index "find(x==1)" method as well but it did not work for me.
But I just showed you that it works with @(x) x(1)
Hi Matt, thanks. I have managed to get it work.

Sign in to comment.

More Answers (0)

Products

Asked:

on 8 Apr 2021

Commented:

on 10 Apr 2021

Community Treasure Hunt

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

Start Hunting!