How can I list individual year from the datetime table?

12 Ansichten (letzte 30 Tage)
Ashfaq Ahmed
Ashfaq Ahmed am 3 Apr. 2023
Beantwortet: Peter Perkins am 5 Apr. 2023
Hi all,
I have a datetime matrix (2003 to 2019, 17 years) and I want to list the range of index for individual years. For example, a cell array named "YearList" that has 17 cells and each of them will contain the date list of individual year. So, all the dates from 2003 will be in the first cell of the array and so on.
I have attached the .mat file. Any feedback will be greatly appreciated!!

Akzeptierte Antwort

Dave B
Dave B am 3 Apr. 2023
The splitapply function makes this pretty easy - here the idea is to split the data by year, and then apply a function that turns a chunk of data into a cell. This approach has the downside (or upside) of only including years that are in your dataset (so 16 cells, because there's no data from 2004).
load("DateList.mat")
YearList=splitapply(@(x){x},Per,findgroups(Per.Year))
YearList = 16×1 cell array
{12348×1 datetime} {15497×1 datetime} {16128×1 datetime} {15360×1 datetime} {16128×1 datetime} {14688×1 datetime} {15168×1 datetime} {15456×1 datetime} {15744×1 datetime} {16896×1 datetime} {16224×1 datetime} {15456×1 datetime} {15552×1 datetime} {16800×1 datetime} {15456×1 datetime} {17760×1 datetime}

Weitere Antworten (1)

Peter Perkins
Peter Perkins am 5 Apr. 2023
I'm gonna go Dave one better:
>> load DateList.mat
>> t = table(Per);
>> t.Year = year(t.Per);
>> t2 = varfun(@(x){x},t,InputVariables="Per",GroupingVariable="Year",OutputFormat="table")
t2 =
16×3 table
Year GroupCount Fun_Per
____ __________ __________________
2003 12348 {12348×1 datetime}
2005 15497 {15497×1 datetime}
2006 16128 {16128×1 datetime}
2007 15360 {15360×1 datetime}
2008 16128 {16128×1 datetime}
2009 14688 {14688×1 datetime}
2010 15168 {15168×1 datetime}
2011 15456 {15456×1 datetime}
2012 15744 {15744×1 datetime}
2013 16896 {16896×1 datetime}
2014 16224 {16224×1 datetime}
2015 15456 {15456×1 datetime}
2016 15552 {15552×1 datetime}
2017 16800 {16800×1 datetime}
2018 15456 {15456×1 datetime}
2019 17760 {17760×1 datetime}

Kategorien

Mehr zu Dates and Time finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by