Reformat and Merge Date and Time columns

조회 수: 3 (최근 30일)
Douglas Leaffer
Douglas Leaffer 2023년 9월 11일
댓글: Stephen23 2023년 11월 2일
Hello. Could use some help merging the Date and Time columns in the attached data file. Date (Column 1) is a DateTime vector dd-MMM-yyyy and Time is a vector that contains 31-Dec-1899 HH:mm:ss What I need is to delete the 31-Dec-1899 default year and merge the true Date (02-Sep-2016) with the remaining Time from column 2. I have tried the following to no avail.
load RVdata.mat
tim = datetime(RVdata.TimeHHmmss, 'InputFormat','dd-MMM-yyyy HH:mm:ss', 'Format','HH:mm:ss')

채택된 답변

Star Strider
Star Strider 2023년 9월 12일
For what it’s worth —
LD = load('RVdata.mat');
data = LD.data
data = 79×6 table
Date TimeHHmmss ObservedSource LAeqmaxdB SourceClass SourceCat ___________ ____________________ ______________________ _________ ___________ _________ 02-Sep-2016 31-Dec-1899 07:54:00 "Budget Truck" 67.3 truck-bus 1 02-Sep-2016 31-Dec-1899 08:00:30 "Trailen Truck" 69.5 truck-bus 1 02-Sep-2016 31-Dec-1899 08:08:50 "Trailen Truck" 67.6 truck-bus 1 02-Sep-2016 31-Dec-1899 08:12:50 "Trailen Truck" 69.5 truck-bus 1 02-Sep-2016 31-Dec-1899 08:14:20 "W.A. Muson Truck" 65.1 truck-bus 1 02-Sep-2016 31-Dec-1899 08:17:02 "Ryclear Truck" 67.1 truck-bus 1 02-Sep-2016 31-Dec-1899 08:18:00 "Shuttle Van" 65.2 truck-bus 1 02-Sep-2016 31-Dec-1899 08:18:40 "Semi Tractor" 74.2 truck-bus 1 02-Sep-2016 31-Dec-1899 08:19:00 "Jeeep (Music)" 67.2 other 0 02-Sep-2016 31-Dec-1899 08:21:01 "Box Truck" 65.1 truck-bus 1 02-Sep-2016 31-Dec-1899 08:22:50 "Siren (Not Observed)" 70.2 other 0 02-Sep-2016 31-Dec-1899 08:23:15 "Siren not olosen" 67.2 other 0 02-Sep-2016 31-Dec-1899 08:29:00 "The Ride" 61.6 truck-bus 1 02-Sep-2016 31-Dec-1899 08:29:40 "SUV" 70.2 other 0 02-Sep-2016 31-Dec-1899 08:30:47 "Motorcycle (Slow)" 62.1 other 0 02-Sep-2016 31-Dec-1899 08:31:30 "Lays Potato Truck" 62.6 truck-bus 1
data.Date = data.Date + timeofday(data.TimeHHmmss);
data = removevars(data,2)
data = 79×5 table
Date ObservedSource LAeqmaxdB SourceClass SourceCat ____________________ ______________________ _________ ___________ _________ 02-Sep-2016 07:54:00 "Budget Truck" 67.3 truck-bus 1 02-Sep-2016 08:00:30 "Trailen Truck" 69.5 truck-bus 1 02-Sep-2016 08:08:50 "Trailen Truck" 67.6 truck-bus 1 02-Sep-2016 08:12:50 "Trailen Truck" 69.5 truck-bus 1 02-Sep-2016 08:14:20 "W.A. Muson Truck" 65.1 truck-bus 1 02-Sep-2016 08:17:02 "Ryclear Truck" 67.1 truck-bus 1 02-Sep-2016 08:18:00 "Shuttle Van" 65.2 truck-bus 1 02-Sep-2016 08:18:40 "Semi Tractor" 74.2 truck-bus 1 02-Sep-2016 08:19:00 "Jeeep (Music)" 67.2 other 0 02-Sep-2016 08:21:01 "Box Truck" 65.1 truck-bus 1 02-Sep-2016 08:22:50 "Siren (Not Observed)" 70.2 other 0 02-Sep-2016 08:23:15 "Siren not olosen" 67.2 other 0 02-Sep-2016 08:29:00 "The Ride" 61.6 truck-bus 1 02-Sep-2016 08:29:40 "SUV" 70.2 other 0 02-Sep-2016 08:30:47 "Motorcycle (Slow)" 62.1 other 0 02-Sep-2016 08:31:30 "Lays Potato Truck" 62.6 truck-bus 1
.
  댓글 수: 3
Douglas Leaffer
Douglas Leaffer 2023년 9월 12일
Thank you both !
Star Strider
Star Strider 2023년 9월 12일
My pleasure!
It make optimal use of the datetime functions.

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

추가 답변 (1개)

William Rose
William Rose 2023년 9월 11일
load('RVdata');
dt1=data.(1)+(3600*hour(data.(2))+60*minute(data.(2))+second(data.(2)))/86400;
dt1.Format='yyyy-MMM-dd hh:mm:ss';
dt1(1:4)
ans = 4×1 datetime array
2016-Sep-02 07:54:00 2016-Sep-02 08:00:30 2016-Sep-02 08:08:49 2016-Sep-02 08:12:50
Try it. Good luck!

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by