MATLAB Answers

how to create a timetable from a datetime array and a matrix?

조회 수: 1(최근 30일)
Doheon Lee
Doheon Lee 2021년 7월 15일
댓글: Walter Roberson 2021년 7월 17일
I like to create a timetable from a datetime array and a matrix.
d = datetime('2021-01-01'):caldays(1):datetime('2021-01-10');
m = rand(10,5);
t = timetable(d', m)
The resulting table is as blow.
Time m
___________ _______________________________________________________
01-Jan-2021 0.95929 0.34998 0.28584 0.12991 0.60198
02-Jan-2021 0.54722 0.1966 0.7572 0.56882 0.26297
03-Jan-2021 0.13862 0.25108 0.75373 0.46939 0.65408
04-Jan-2021 0.14929 0.61604 0.38045 0.011902 0.68921
05-Jan-2021 0.25751 0.47329 0.56782 0.33712 0.74815
06-Jan-2021 0.84072 0.35166 0.075854 0.16218 0.45054
07-Jan-2021 0.25428 0.83083 0.05395 0.79428 0.083821
08-Jan-2021 0.81428 0.58526 0.5308 0.31122 0.22898
09-Jan-2021 0.24352 0.54972 0.77917 0.52853 0.91334
10-Jan-2021 0.92926 0.91719 0.93401 0.16565 0.15238
But, I like to create the resulting table with 5 columns as below.
Time A B C D E
___________ _______________________________________________________
01-Jan-2021 0.95929 0.34998 0.28584 0.12991 0.60198
02-Jan-2021 0.54722 0.1966 0.7572 0.56882 0.26297
03-Jan-2021 0.13862 0.25108 0.75373 0.46939 0.65408
04-Jan-2021 0.14929 0.61604 0.38045 0.011902 0.68921
05-Jan-2021 0.25751 0.47329 0.56782 0.33712 0.74815
06-Jan-2021 0.84072 0.35166 0.075854 0.16218 0.45054
07-Jan-2021 0.25428 0.83083 0.05395 0.79428 0.083821
08-Jan-2021 0.81428 0.58526 0.5308 0.31122 0.22898
09-Jan-2021 0.24352 0.54972 0.77917 0.52853 0.91334
10-Jan-2021 0.92926 0.91719 0.93401 0.16565 0.15238
So I tried the following code to give different names to each column, but it does not work.
t.Properties.VariableNames = {'A', 'B', 'C', 'D', 'E'}
Thank you very much for the help in advance.

채택된 답변

Walter Roberson
Walter Roberson 2021년 7월 15일
편집: Walter Roberson 2021년 7월 15일
d = datetime('2021-01-01'):caldays(1):datetime('2021-01-10');
m = rand(10,5);
t = array2table(m, 'VariableNames', {'A', 'B', 'C', 'D', 'E'})
t = 10×5 table
A B C D E ________ ________ _________ ________ _______ 0.98979 0.91374 0.2907 0.16664 0.85502 0.037379 0.19918 0.45673 0.96181 0.55476 0.16478 0.66507 0.72775 0.66474 0.44108 0.96658 0.74907 0.096927 0.34676 0.32186 0.33786 0.033472 0.94875 0.84561 0.43635 0.62363 0.82431 0.50066 0.99156 0.50445 0.76383 0.24839 0.37759 0.31366 0.54562 0.26933 0.50286 0.98601 0.020343 0.39307 0.53943 0.27297 0.47952 0.30908 0.33387 0.38275 0.31086 0.0038893 0.29516 0.34232
tt = table2timetable(t, 'RowTimes', d.')
tt = 10×5 timetable
Time A B C D E ___________ ________ ________ _________ ________ _______ 01-Jan-2021 0.98979 0.91374 0.2907 0.16664 0.85502 02-Jan-2021 0.037379 0.19918 0.45673 0.96181 0.55476 03-Jan-2021 0.16478 0.66507 0.72775 0.66474 0.44108 04-Jan-2021 0.96658 0.74907 0.096927 0.34676 0.32186 05-Jan-2021 0.33786 0.033472 0.94875 0.84561 0.43635 06-Jan-2021 0.62363 0.82431 0.50066 0.99156 0.50445 07-Jan-2021 0.76383 0.24839 0.37759 0.31366 0.54562 08-Jan-2021 0.26933 0.50286 0.98601 0.020343 0.39307 09-Jan-2021 0.53943 0.27297 0.47952 0.30908 0.33387 10-Jan-2021 0.38275 0.31086 0.0038893 0.29516 0.34232
.... or
t = timetable('RowTimes', d.')
t = 10×0 empty timetable
t.A = m(:,1); t.B = m(:,2); t.C = m(:,3); t.D = m(:,4); t.E = m(:,5);
t
t = 10×5 timetable
Time A B C D E ___________ ________ ________ _________ ________ _______ 01-Jan-2021 0.98979 0.91374 0.2907 0.16664 0.85502 02-Jan-2021 0.037379 0.19918 0.45673 0.96181 0.55476 03-Jan-2021 0.16478 0.66507 0.72775 0.66474 0.44108 04-Jan-2021 0.96658 0.74907 0.096927 0.34676 0.32186 05-Jan-2021 0.33786 0.033472 0.94875 0.84561 0.43635 06-Jan-2021 0.62363 0.82431 0.50066 0.99156 0.50445 07-Jan-2021 0.76383 0.24839 0.37759 0.31366 0.54562 08-Jan-2021 0.26933 0.50286 0.98601 0.020343 0.39307 09-Jan-2021 0.53943 0.27297 0.47952 0.30908 0.33387 10-Jan-2021 0.38275 0.31086 0.0038893 0.29516 0.34232
.... or
t = [timetable('RowTimes', d.'), array2table(m, 'VariableNames', {'A', 'B', 'C', 'D', 'E'})]
t = 10×5 timetable
Time A B C D E ___________ ________ ________ _________ ________ _______ 01-Jan-2021 0.98979 0.91374 0.2907 0.16664 0.85502 02-Jan-2021 0.037379 0.19918 0.45673 0.96181 0.55476 03-Jan-2021 0.16478 0.66507 0.72775 0.66474 0.44108 04-Jan-2021 0.96658 0.74907 0.096927 0.34676 0.32186 05-Jan-2021 0.33786 0.033472 0.94875 0.84561 0.43635 06-Jan-2021 0.62363 0.82431 0.50066 0.99156 0.50445 07-Jan-2021 0.76383 0.24839 0.37759 0.31366 0.54562 08-Jan-2021 0.26933 0.50286 0.98601 0.020343 0.39307 09-Jan-2021 0.53943 0.27297 0.47952 0.30908 0.33387 10-Jan-2021 0.38275 0.31086 0.0038893 0.29516 0.34232
  댓글 수: 2
Walter Roberson
Walter Roberson 2021년 7월 17일
Ah, that is a good approach!

댓글을 달려면 로그인하십시오.

추가 답변(0개)

태그

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by