Average of a structure

조회 수: 21 (최근 30일)
Sreejoe Kaniyamparambil
Sreejoe Kaniyamparambil 2017년 2월 10일
편집: Stephen23 2017년 2월 10일
Hey everyonone. I'd like to get the average of the same entry of each field inside a struct field (here: 'thickness') . This is what my data looks like :
To make it more exact I'd like to have a code to give me the following output:
avg(1)=mean(files.meas_data(1-9).thickness(1)
avg(2)=mean(files.meas_data(1-9).thickness(2)
and so on. I tried it using nested loops:
[
for k=1:length(files.meas_data.thickness)
for l=1:length(files.files_during)
avg(k)=mean(files.meas_data(l).thickness(k))
end
end
]
However I always get the following error message:
files =
all: {18x1 cell}
files_during: {9x1 cell}
meas_data: [1x9 struct]
Error using length Too many input arguments.
Error in RunCode (line 11) for k=1:length(files.meas_data.thickness)
Appreciate your help very much. Please bear with me I just started matlab today.

채택된 답변

Stephen23
Stephen23 2017년 2월 10일
편집: Stephen23 2017년 2월 10일
Perhaps using arrayfun::
arrayfun(@(s)mean(s.thickness),files.meas_data)
S = files.meas_data;
cellfun(@mean,{S.thickness})
  댓글 수: 2
Sreejoe Kaniyamparambil
Sreejoe Kaniyamparambil 2017년 2월 10일
Hey Stephen, Thank you. Unfortunatley this command gives me the average of each all the values inside a thickness field. I need the mean of the same entry in 'thickness' for all the thicknesses(9)
Stephen23
Stephen23 2017년 2월 10일
편집: Stephen23 2017년 2월 10일
The thickness arrays have different sizes. How will you take the 107th value from meas_data(2).thickness, which only has 95 elements ?
You might be able to use Jos's excellent FEX submission padcat, something like this (untested):
S = files.meas_data;
M = padcat(S.thickness);
nanmean(M,1)
Or perhaps, one at a time:
N = 6;
mean(cellfun(@(v)v(N),{S.thickness}))

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Structures에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by