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;
採用された回答
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
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
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
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
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.
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
2022 年 2 月 21 日
Idk what happened, try with this new attachement...
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
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
2022 年 2 月 21 日
Thank you!
As always, my pleasure!
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Time Series Objects についてさらに検索
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
