splitapply with own created function

조회 수: 11 (최근 30일)
Senne Van Minnebruggen
Senne Van Minnebruggen 2020년 4월 1일
댓글: Senne Van Minnebruggen 2020년 4월 2일
I have 'nr' tables containing the following: date/time/Qdot/temperature
i would like to apply a function to this tables more specific i would like to apply a function on data grouped by day (date)
herefor i've tried to use the function splitapply where the function is of my own creating
for j=2:24
s = (j-1); %sizetimestep - 1stap
for i=1:nr
[G,days] = findgroups(DayT{i}.date);
max_Qdot = splitapply(@maxperinterval,DayT{i}.Qdot,s,G);
Mean_T = splitapply(@mean,DayT{i}.Temperature,G);
Max_tabel{j,i}=table(days,max_Qdot,Mean_T,'VarbiableNames',rownames);
end
end
But when i tried to run this i get an error saying the following:
"The data variables must have the same number of rows as the vector of group numbers. The group number vector
has 5736 row(s), and data variable 2 has 1 row(s)."
My own function needs the input of variable s.
Is this even possible to use this function with splitapply?

채택된 답변

Rik
Rik 2020년 4월 1일
편집: Rik 2020년 4월 1일
You can probably get around this by wrapping your function in an anonymous function:
fun=@(input1)maxperinterval(input1,s);
max_Qdot = splitapply(fun,DayT{i}.Qdot,G);
Now the function splitapply is looking at only requires 1 input variable while your function will receive both. Note that you will need to generate fun every time you update s.

추가 답변 (1개)

Ameer Hamza
Ameer Hamza 2020년 4월 1일
As the error indicate. The error is related to the orientation of matrices. Change it like this
max_Qdot = splitapply(@maxperinterval,DayT{i}.Qdot,s.',G); % use transpose of s so that it become column matrix

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by