필터 지우기
필터 지우기

Grabbing Years of table data to Create New Tables

조회 수: 1 (최근 30일)
Josh Morell
Josh Morell 2020년 8월 28일
댓글: Peter O 2020년 8월 29일
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
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
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 CenterFile Exchange에서 Other Formats에 대해 자세히 알아보기

태그

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by