How can I list individual year from the datetime table?
조회 수: 12 (최근 30일)
이전 댓글 표시
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
Walter Roberson
2023년 4월 3일
See https://www.mathworks.com/matlabcentral/answers/1940499-how-to-get-the-indices-based-on-month-and-year#answer_1207779 but you would only need the y output of ymd
채택된 답변
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).
load("DateList.mat")
YearList=splitapply(@(x){x},Per,findgroups(Per.Year))
추가 답변 (1개)
Peter Perkins
2023년 4월 5일
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}
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Dates and Time에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!