splitapply with own created function
11 views (last 30 days)
Show older comments
Senne Van Minnebruggen
on 1 Apr 2020
Commented: Senne Van Minnebruggen
on 2 Apr 2020
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?
0 Comments
Accepted Answer
Rik
on 1 Apr 2020
Edited: Rik
on 1 Apr 2020
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.
More Answers (1)
Ameer Hamza
on 1 Apr 2020
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
0 Comments
See Also
Categories
Find more on String Parsing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!