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

6 visualizaciones (últimos 30 días)
Ashfaq Ahmed el 3 de Abr. de 2023
Respondida: Peter Perkins el 5 de Abr. de 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!!
##### 1 comentarioMostrar NingunoOcultar Ninguno
Walter Roberson el 3 de Abr. de 2023

Iniciar sesión para comentar.

Dave B el 3 de Abr. de 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).
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 comentarioMostrar NingunoOcultar Ninguno
Ashfaq Ahmed el 4 de Abr. de 2023
Thank you so much!!

Iniciar sesión para comentar.

### Más respuestas (1)

Peter Perkins el 5 de Abr. de 2023
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}
##### 0 comentariosMostrar -1 comentarios más antiguosOcultar -1 comentarios más antiguos

Iniciar sesión para comentar.

### Categorías

Más información sobre Dates and Time en Help Center y File Exchange.

### Community Treasure Hunt

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

Start Hunting!

Translated by