MATLAB Answers

Mean of a field of a struct

11 views (last 30 days)
FG
FG on 13 Sep 2020
Commented: Ameer Hamza on 15 Sep 2020
As seen in image below, I have a struct of 5 fields. I need to compute the avarege of the HV3, HV5, HV7 and thd in 1st row, then in the second row and so on seperately.. Have a nice weekend..

Answers (2)

Matt J
Matt J on 13 Sep 2020
C=struct2cell(rmfield(S,'name'));
A=cell2mat(reshape(C,1,4,[]));
Averages = reshape( mean(A,1),4,[]).'
  3 Comments
FG
FG on 14 Sep 2020
Thanks Matt..

Sign in to comment.


Ameer Hamza
Ameer Hamza on 13 Sep 2020
Try something like this
S_new = rmfield(S, 'name');
S_out = arrayfun(@(s) structfun(@mean, s, 'uni', 0), S_new);
[S_out.name] = S.name;
  8 Comments
Ameer Hamza
Ameer Hamza on 15 Sep 2020
I am glad to be of help!

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!