Extract values from a cell based on dates

조회 수: 3 (최근 30일)
Manuel Flores
Manuel Flores 2018년 10월 7일
편집: jonas 2018년 10월 9일
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
jonas
jonas 2018년 10월 8일
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 2018년 10월 8일
Thanks everyone I'm taking the suggestions mentioned and importing it as a table.

댓글을 달려면 로그인하십시오.

채택된 답변

jonas
jonas 2018년 10월 8일
편집: jonas 2018년 10월 8일
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
Manuel Flores
Manuel Flores 2018년 10월 9일
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 2018년 10월 9일
편집: jonas 2018년 10월 9일
Easier would be
TT(day(TT.Time)==28,:)

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Data Type Identification에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by