Extract values from a cell based on dates

3 visualizaciones (últimos 30 días)
Manuel Flores
Manuel Flores el 7 de Oct. de 2018
Editada: jonas el 9 de Oct. de 2018
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 comentarios
jonas
jonas el 8 de Oct. de 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.
Manuel Flores
Manuel Flores el 8 de Oct. de 2018
Thanks everyone I'm taking the suggestions mentioned and importing it as a table.

Iniciar sesión para comentar.

Respuesta aceptada

jonas
jonas el 8 de Oct. de 2018
Editada: jonas el 8 de Oct. de 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.
  7 comentarios
Manuel Flores
Manuel Flores el 9 de Oct. de 2018
Hey gents I have another question.
I have a code like this where I pull the data based on those 4 dates. That works, so no problem there.
% Pull the Temperature from the design dates.
Tr = {'2017-07-01','2017-07-02','2017-08-01','2017-08-02'};
Tj = TT_all(Tr,2);
Now I'm wondering how to do the same for 28 day for every month over 12 months. Obviously it's not like this.
Tr_all = {'2017-07-01:2017-07-28','2017-08-02:2017-08-28','2017-09-01:2017-09-28'}...
{'2017-10-01:2017-10-28','2017-11-01:2017-11-28','2017-12-01:2017-12-28'}...
{'2018-01-01:2011-01-28','2018-02-01:2011-02-28','2018-03-01:2011-03-28'}...
{'2018-04-01:2011-04-28','2018-05-01:2011-05-28','2018-06-01:2011-06-28'};
can you point me in the right direction please. Thanks
jonas
jonas el 9 de Oct. de 2018
Editada: jonas el 9 de Oct. de 2018
Easier would be
TT(day(TT.Time)==28,:)

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Data Type Identification en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by