splitapply with own created function

7 次查看(过去 30 天)
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