Combine date and duration into single datetime column

Hello!
I have a vector containing the data in a datetime format (MM/dd/uuuu)and the vector containing the hours of the days in duration format(hh:mm:ss)
I want to combine the two vectors in order to get a single datetime vector having date and hour togheter.
I tried this code but it seems that is not working:
new_datetime=date+hour;

 採用された回答

Star Strider
Star Strider 2022 年 2 月 21 日

0 投票

Add them —
DT = datetime(repmat([2018 01 01], 5, 1))
DT = 5×1 datetime array
01-Jan-2018 01-Jan-2018 01-Jan-2018 01-Jan-2018 01-Jan-2018
DU = duration('00:00:00') + 5*minutes(0:4)'
DU = 5×1 duration array
00:00:00 00:05:00 00:10:00 00:15:00 00:20:00
Combined = DT + DU
Combined = 5×1 datetime array
01-Jan-2018 00:00:00 01-Jan-2018 00:05:00 01-Jan-2018 00:10:00 01-Jan-2018 00:15:00 01-Jan-2018 00:20:00
.

8 件のコメント

Enrico Gambini
Enrico Gambini 2022 年 2 月 21 日
I tried. Your code works.
But this approach it is not working with my own data and I don't know why.
Got a 420002x1 datetime array and a 420002x1 duration array. I add them and it remains only the date not followed by the hours
Star Strider
Star Strider 2022 年 2 月 21 日
Please attach a sample of your data, preferably as a text file, since it is difficult to work with .mat files with the online Run feature here.
DT = datetime(repmat([2018 01 01], 5, 1))
DT = 5×1 datetime array
01-Jan-2018 01-Jan-2018 01-Jan-2018 01-Jan-2018 01-Jan-2018
DU = duration('00:00:00') + 5*minutes(0:4)'
DU = 5×1 duration array
00:00:00 00:05:00 00:10:00 00:15:00 00:20:00
T1 = table(DT,DU, 'VariableNames',{'date','hour'})
T1 = 5×2 table
date hour ___________ ________ 01-Jan-2018 00:00:00 01-Jan-2018 00:05:00 01-Jan-2018 00:10:00 01-Jan-2018 00:15:00 01-Jan-2018 00:20:00
T1.Combined = T1{:,1} + T1{:,2} % Cell Reference
T1 = 5×3 table
date hour Combined ___________ ________ ____________________ 01-Jan-2018 00:00:00 01-Jan-2018 00:00:00 01-Jan-2018 00:05:00 01-Jan-2018 00:05:00 01-Jan-2018 00:10:00 01-Jan-2018 00:10:00 01-Jan-2018 00:15:00 01-Jan-2018 00:15:00 01-Jan-2018 00:20:00 01-Jan-2018 00:20:00
T1 = T1(:,1:2)
T1 = 5×2 table
date hour ___________ ________ 01-Jan-2018 00:00:00 01-Jan-2018 00:05:00 01-Jan-2018 00:10:00 01-Jan-2018 00:15:00 01-Jan-2018 00:20:00
T1.Combined = T1.date + T1.hour % Variable Name Reference
T1 = 5×3 table
date hour Combined ___________ ________ ____________________ 01-Jan-2018 00:00:00 01-Jan-2018 00:00:00 01-Jan-2018 00:05:00 01-Jan-2018 00:05:00 01-Jan-2018 00:10:00 01-Jan-2018 00:10:00 01-Jan-2018 00:15:00 01-Jan-2018 00:15:00 01-Jan-2018 00:20:00 01-Jan-2018 00:20:00
I am not certain how you are combining them as table variables, so considering that could be the problem, I offer these examples that both lead to a correct result.
.
Enrico Gambini
Enrico Gambini 2022 年 2 月 21 日
Yes, thank you. I'll attach the txt file.
I would like to merge "date" and "hour" columns.
Thank you very much
Stephen23
Stephen23 2022 年 2 月 21 日
T = readtable('T.txt')
Warning: Column headers from the file were modified to make them valid MATLAB identifiers before creating variable names for the table. The original column headers are saved in the VariableDescriptions property.
Set 'VariableNamingRule' to 'preserve' to use the original column headers as table variable names.
T = 200000×1 table
dateHour ___________________ 01/01/2018 00:00:00 01/01/2018 00:05:00 01/01/2018 00:10:00 01/01/2018 00:15:00 01/01/2018 00:20:00 01/01/2018 00:25:00 01/01/2018 00:30:00 01/01/2018 00:35:00 01/01/2018 00:40:00 01/01/2018 00:45:00 01/01/2018 00:50:00 01/01/2018 00:55:00 01/01/2018 01:00:00 01/01/2018 01:05:00 01/01/2018 01:10:00 01/01/2018 01:15:00
Enrico Gambini
Enrico Gambini 2022 年 2 月 21 日
Idk what happened, try with this new attachement...
Star Strider
Star Strider 2022 年 2 月 21 日
編集済み: Star Strider 2022 年 2 月 21 日
They appear to be merged in the file! (The ‘new attachment’ read here in the edited Comment.)
T1 = readtable('https://www.mathworks.com/matlabcentral/answers/uploaded_files/902050/T.txt', 'VariableNamingRule','preserve')
T1 = 200000×1 table
date→hour ___________________ 01/01/2018 00:00:00 01/01/2018 00:05:00 01/01/2018 00:10:00 01/01/2018 00:15:00 01/01/2018 00:20:00 01/01/2018 00:25:00 01/01/2018 00:30:00 01/01/2018 00:35:00 01/01/2018 00:40:00 01/01/2018 00:45:00 01/01/2018 00:50:00 01/01/2018 00:55:00 01/01/2018 01:00:00 01/01/2018 01:05:00 01/01/2018 01:10:00 01/01/2018 01:15:00
What else is there to do?
.
Enrico Gambini
Enrico Gambini 2022 年 2 月 21 日
I really don't know how this is possible. I simply exported the two columns of the table and they have been automatically merged... hence, I will use this datetime in table "T" for my work.
Neverthless, your answer should have worked and I'm gonna accept it.
Thank you for your patience!
Star Strider
Star Strider 2022 年 2 月 21 日
Thank you!
As always, my pleasure!

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeTime Series Objects についてさらに検索

製品

リリース

R2021a

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by