Total Hour Conversion to Date and Time
Show older comments
Hi,
I was having difficulties when originally trying to convert with datenum, so I manually converted all of my time values to hours(Figure 2). The format was originally in month/day/year hour:minute AM, as can be seen in Figure 1. I now have a slope value(seen in Fig 3) which I need to reconvert back to the original format; however, it signifies a time in the future. I dont know how to manually convert back as the day/month/year hour are all included in the total hour value, and I am having trouble distinguishing between a month or day. If there is an easy fix to convert back, I would appreciate the feedback.
Figure 1

Figure 2

Figure 3

3 Comments
dpb
on 7 Aug 2019
As another case just answered on slightly different subject, let's go back and fix the original problem instead of trying to clean up a mess later.
Attach a short section of the input file with the original date strings so we don't have to make it up for ourselves...
Felicia Lepore
on 7 Aug 2019
Edited: Felicia Lepore
on 7 Aug 2019
Star Strider
on 7 Aug 2019
Please also post the code to read the file and assign the variables you use.
Answers (2)
dpb
on 7 Aug 2019
t=readtable('hc07.xlsx');
leaves me with
>> t(1:5,:)
ans =
5×21 table
RECORDED CH04_HC07_CZ_SD_Corrected_rel CH04_HC07_3D_Movement_rel CH04_HC07_Horizontal_Movement_rel CH04_HC07_Elevation_Change_rel Var6 hours hourDiff disDiff du_dt t_du_dt Var12 Var13 Var14 Var15 Var16 Var17 Var18 Var19 Var20 Var21
________________________ _____________________________ _________________________ _________________________________ ______________________________ _________ __________ ________ _________ __________ _______ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____
8/2/2018 12:39:17.372 AM 0 0 0 NaN 0 1.7683e+07 0 0 0 0 NaN NaN NaN NaN NaN NaN NaN NaN NaN ''
8/2/2018 2:27:57.046 AM 0.0030494 0.081832 0.080672 -0.013735 0.0030494 1.7683e+07 2 0.0030494 0.0015247 26974 NaN NaN NaN NaN NaN NaN NaN NaN NaN ''
8/2/2018 4:41:25.817 AM 0.0047692 0.053733 0.053672 -0.0025604 0.0078186 1.7683e+07 2 0.0047692 0.0023846 42187 NaN NaN NaN NaN NaN NaN NaN NaN NaN ''
8/2/2018 6:38:34.026 AM 0.0018315 0.11069 0.10569 0.032892 0.0096501 1.7683e+07 2 0.0018315 0.00091577 16201 NaN NaN NaN NaN NaN NaN NaN NaN NaN ''
8/2/2018 8:36:15.693 AM 0.0032632 0.10986 0.095714 -0.053936 0.012913 1.7683e+07 2 0.0032632 0.0016316 28865 NaN NaN NaN NaN NaN NaN NaN NaN NaN ''
>> t.RECORDED(1)
ans =
datetime
8/2/2018 12:39:17.372 AM
>>
readtable converted to datetime() on input for you...
If you used xlsread instead, the text dates were only to second precision...
>> [x,t,r]=xlsread('HC07.xlsx');
>> t(1:5,1)
ans =
5×1 cell array
{'RECORDED' }
{'8/2/2018 12:39:17 AM'}
{'8/2/2018 2:27:57 AM' }
{'8/2/2018 4:41:26 AM' }
{'8/2/2018 6:38:34 AM' }
>> dt=datetime(t(2:5,1),'InputFormat','M/d/uuuu h:m:s a')
dt =
4×1 datetime array
02-Aug-2018 00:39:17
02-Aug-2018 02:27:57
02-Aug-2018 04:41:26
02-Aug-2018 06:38:34
>>
readtable read the Excel date and returned full precision; xlsread returned only the displayed text to seconds as text so the conversion has to match...
Either way, you end up w/ datetime() from the git-go...whether you need the full precision will predicate how you read the file or you'll have to add the extra precision to the display
Felicia Lepore
on 7 Aug 2019
0 votes
1 Comment
Guillaume
on 7 Aug 2019
Please don't use Answer this question to ask a new question.
It is trivial to convert a duration into a plain numeric type with the days, hours, minutes, seconds, etc. function depending on which unit you want for your numeric type:
>> d = duration('5:12:6:30'); %a duration of 5 days, 12 hours, 6 minutes, 30 seconds
>> years(d) %convert to years
ans =
0.0150708471464544
>> days(d) %convert to days
ans =
5.50451388888889
>> hours(d) %convert to hours
ans =
132.108333333333
>> minutes(d) %convert to minutes
ans =
7926.5
>> seconds(d) %convert to seconds
ans =
475590
Categories
Find more on Dates and Time in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!