# How can I list individual year from the datetime table?

5 ビュー (過去 30 日間)
Ashfaq Ahmed 2023 年 4 月 3 日

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!!

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

### 採用された回答

Dave B 2023 年 4 月 3 日
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).
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}
##### 1 件のコメント-1 件の古いコメントを表示-1 件の古いコメントを非表示
Ashfaq Ahmed 2023 年 4 月 4 日
Thank you so much!!

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

### その他の回答 (1 件)

Peter Perkins 2023 年 4 月 5 日
I'm gonna go Dave one better:
>> 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}

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

### カテゴリ

Help Center および File ExchangeDates and Time についてさらに検索

### Community Treasure Hunt

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

Start Hunting!

Translated by