How do I split a MATLAB table into seperate tables by date?
    조회 수: 6 (최근 30일)
  
       이전 댓글 표시
    
I have a table with a datatime and temperature column that I want to split into seperate tables based on date. For example, I want to split the below table into 3 sperate tables because there are 3 different days in the table.
Date                    Temperature
2022-05-21            12
2021-05-21            19
2022-05-21            24
2022-06-01            21
2022-06-01            22
2022-06-04            25
How do I do this?
댓글 수: 0
채택된 답변
  Voss
      
      
 2022년 7월 20일
        Here's one way:
t = table( ...
    {'2022-05-21';'2021-05-21';'2022-05-21';'2022-06-01';'2022-06-01';'2022-06-04'}, ...
    [12;19;24;21;22;25], ...
    'VariableNames',{'Date' 'Temperature'})
[uu,~,jj] = unique(t.Date);
nuu = numel(uu);
c = cell(nuu,1);
for ii = 1:nuu
    c{ii} = t(jj == ii,:);
end
c
c{:}
Looks like 4 different dates, but maybe the 2021 is a typo.
추가 답변 (1개)
  Walter Roberson
      
      
 2022년 7월 21일
        t = table( ...
    {'2022-05-21';'2021-05-21';'2022-05-21';'2022-06-01';'2022-06-01';'2022-06-04'}, ...
    [12;19;24;21;22;25], ...
    'VariableNames',{'Date' 'Temperature'})
G = findgroups(t.Date);
c = splitapply(@(varargin) {table(varargin{:}, 'VariableNames', t.Properties.VariableNames)}, t, G);
c
참고 항목
카테고리
				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!