Extract values from a cell based on dates
2 views (last 30 days)
Show older comments
Hi all,
I have data in 6 separate cells (A, B, C, D, E, F), all which are 365x1. One of these cells (A) contains dates as 'datenum' for a whole 12 months. My question is how can I call/pull/extract data from these cells based on date requirements and get an average. i.e. all of the values on the 10th for each month.
Would it be best to use stack and place them all as a table? I don't have any code for this yet as I don't know where to begin.
7 Comments
jonas
on 8 Oct 2018
As was suggested several times already, what you want is a timetable and retime. If you want to calculate some statistics based on the "day of month", then I would suggest using the day of month as a grouping variable. This is easy if you first convert your data to datetime format, which you should do anyway.
Accepted Answer
jonas
on 8 Oct 2018
Edited: jonas
on 8 Oct 2018
Here's another approach
%%Dummy dataset
Date = datetime('2000-01-01')+days(0:364);
Date.Format = 'dd/MMM/yyyy';
Data = rand(numel(Date),5);
%%Data to timetable
TT = timetable(Date',day(Date)',Data)
TT = splitvars(TT)
TT.Properties.VariableNames = {'DayOfMonth','C1','C2','C3','C4','C5'}
TT =
366×6 timetable
Time DayOfMonth C1 C2 C3 C4 C5
___________ __________ _________ __________ __________ _________ _________
01/Jan/2000 1 0.83516 0.64556 0.79434 0.73071 0.068773
02/Jan/2000 2 0.11948 0.47084 0.92745 0.75217 0.19456
03/Jan/2000 3 0.76848 0.28703 0.54273 0.59603 0.94978
...
Continuing...
%%Mean, std and max with DayOfMonth as grouping variable
[s1,s2,s3] = grpstats(TT.Variables,TT.DayOfMonth,{'mean','std','max'})
%%Monthly average using retime
TT2 = retime(TT,'monthly','mean')
s1, s2 and s3 are matrices with 31 rows representing the selected statistics for each day of the month.
More Answers (0)
See Also
Categories
Find more on Data Type Identification 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!