How do I split a MATLAB table into seperate tables by date?

7 ビュー (過去 30 日間)
Bhavishey Thapar
Bhavishey Thapar 2022 年 7 月 20 日
コメント済み: Voss 2022 年 7 月 25 日
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?

採用された回答

Voss
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'})
t = 6×2 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
[uu,~,jj] = unique(t.Date);
nuu = numel(uu);
c = cell(nuu,1);
for ii = 1:nuu
c{ii} = t(jj == ii,:);
end
c
c = 4×1 cell array
{1×2 table} {2×2 table} {2×2 table} {1×2 table}
c{:}
ans = 1×2 table
Date Temperature ______________ ___________ {'2021-05-21'} 19
ans = 2×2 table
Date Temperature ______________ ___________ {'2022-05-21'} 12 {'2022-05-21'} 24
ans = 2×2 table
Date Temperature ______________ ___________ {'2022-06-01'} 21 {'2022-06-01'} 22
ans = 1×2 table
Date Temperature ______________ ___________ {'2022-06-04'} 25
Looks like 4 different dates, but maybe the 2021 is a typo.
  2 件のコメント
Bhavishey Thapar
Bhavishey Thapar 2022 年 7 月 25 日
Thank you.
Voss
Voss 2022 年 7 月 25 日
You're welcome!

サインインしてコメントする。

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2022 年 7 月 21 日
Thanks to @Voss for inputting the data.
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'})
t = 6×2 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
G = findgroups(t.Date);
c = splitapply(@(varargin) {table(varargin{:}, 'VariableNames', t.Properties.VariableNames)}, t, G);
c
c = 4×1 cell array
{1×2 table} {2×2 table} {2×2 table} {1×2 table}

カテゴリ

Help Center および File ExchangeDates and Time についてさらに検索

タグ

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by