Grabbing Years of table data to Create New Tables
1 次查看(过去 30 天)
显示 更早的评论
I have a table with two collumns. The first collumn is dates. The second collumn is the data. Is there an easy way to grab all the data from each year and create new tables?
回答(1 个)
Peter O
2020-8-29
A fast solution might be to use the Timestamp to extract the date for each row and then use the group capabilities of tables.
[G, yrs] = findgroups(year(T.date))
Then you can iterate through each group, to get new tables. I'll place them into cells here, for simplicity and extnesibility. If you have a large number of years you may want to preallocate the cell.
newTablesByYear = cell(numel(yrs),1)
for ix=1:numel(yrs)
newTablesByYear{ix} = T(G==ix,:)
end
1 个评论
Peter O
2020-8-29
- This solution assumes the first column is formatted as a datetime. If it's a date string then you may have to do a little extra formatting on the input before the YEAR() function knows what to do with it. See the help on datetime and datestr for some guidance.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Other Formats 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!