Using timetable to sort and apply functions to data with a timestep greater than one year

1 回表示 (過去 30 日間)
I am working with daily temperature data; min, max, and mean over a period of 40 years. I have no problem using the timetable functionality to sort the data, by year, month,etc, however I want to compare the data for the lifetime and find the standard deviation and median. I know this is possible to do using arrays and for loops to sort and group the data, but because I have it set-up with timetables already I think it would be more elegant? Using the retime function I could change the timestep, but getting the appropriate data from that didn't work.
Thanks!

採用された回答

Ameer Hamza
Ameer Hamza 2020 年 12 月 2 日
If you mean that instead of regular time steps, e.g., monthly, yearly, etc. You want to use a custom time-step, you can use the following signature of retime() function
TT2 = retime(TT1,'regular',method,'TimeStep',dt)
here dt can be defined using duration().
  9 件のコメント
Steven Lord
Steven Lord 2020 年 12 月 2 日
dt = datetime(2000,1,1):datetime(2010,12,31);
data = rand(size(dt));
TT = timetable(dt.', data.', 'VariableNames', {'data'});
I'd use groupsummary for this. To get the mean of the data for each month and year combination in the timetable's Time:
resultPerMonthAndYear = groupsummary(TT, 'Time', 'month', @mean)
resultPerMonthAndYear = 132x3 table
month_Time GroupCount fun1_data __________ __________ _________ Jan-2000 31 0.44937 Feb-2000 29 0.48499 Mar-2000 31 0.43828 Apr-2000 30 0.49082 May-2000 31 0.51453 Jun-2000 30 0.55288 Jul-2000 31 0.52957 Aug-2000 31 0.49907 Sep-2000 30 0.47676 Oct-2000 31 0.56463 Nov-2000 30 0.49248 Dec-2000 31 0.49151 Jan-2001 31 0.50908 Feb-2001 28 0.47816 Mar-2001 31 0.43094 Apr-2001 30 0.42253
To get the mean of the data for each month across all the years in the data set:
resultPerMonth = groupsummary(TT, 'Time', 'monthofyear', @mean)
resultPerMonth = 12x3 table
monthofyear_Time GroupCount fun1_data ________________ __________ _________ 1 341 0.50765 2 311 0.5124 3 341 0.49826 4 330 0.48444 5 341 0.48446 6 330 0.51394 7 341 0.4952 8 341 0.52894 9 330 0.5359 10 341 0.49456 11 330 0.51517 12 341 0.47285
SCampagnolo
SCampagnolo 2020 年 12 月 2 日
Okay that makes sense also, the only difference being it is easier to apply a bulk function to the splitapply function to get an output of all of the data in one table without setting up a loop.
function [lo,avg,med,mo,hi,standard] = multiStatCalc(x)
lo = min(x);
avg = mean(x);
med=median(x);
mo=mode(x);
hi = max(x);
standard=std(x);
end
Like this
dt = datetime(2000,1,1):datetime(2010,12,31);
data = rand(size(dt));
TT = timetable(dt.', data.', 'VariableNames', {'data'});
[grps, ~] = findgroups(month(TT.Time));
[Lmin,Lmean,Lmedian,Lmode,LMax,Lstd] = splitapply(@multiStatCalc, TT.data, grps);
Lresult=table(Lmin,Lmean,Lmedian,Lmode,LMax,Lstd);
The challenge I'm having now is MATLAB won't let me use the splitapply function in desktop version, I get the error:'Attempt to execute SCRIPT splitapply as a function:.' The script works fine in the MATLAB Online editor, so I'm thinking it has something to do with my setup.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeTables についてさらに検索

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by