Convert time in decimal days into hh:mm:ss format
35 views (last 30 days)
Show older comments
Hi all,
Just encounter some issue regarding converting time logs from excel
when I used readtable functrion the time from excel was hh:mm:ss and ocnverted at MATLAB to decimal in days
for example: 12:06:30 in excel was converted to 0.504513888888889 in MATLAB.
so how can I conerted to hh:nn:ss?
0 Comments
Answers (2)
Stephen23
on 5 Dec 2021
Edited: Stephen23
on 5 Dec 2021
N = 0.504513888888889;
T = days(N);
T.Format = 'hh:mm:ss'
or
D = datetime(N,'ConvertFrom','excel');
T = timeofday(D)
11 Comments
Stephen23
on 6 Dec 2021
Zach Morag's incorrectly posted "Answer" moved here:
if Im using days, as you wrote, the output is this:
and if Im using datetime:
scaleLog.('Time [hh:mm:ss]') = datetime(scaleLog{:,2},'ConvertFrom','excel');
scaleLog{:,2}.Format = "HH:mm:ss";
the output is that:
How can I remove the date and remain only with the time?
Stephen23
on 6 Dec 2021
Edited: Stephen23
on 6 Dec 2021
@Zach Morag: Yes, can I remember meeting this exact issue a few years ago. Apparently it is not possible to use curly-brace indexing to change the format of an already existing datetime variable in a table:
D = datetime(2021,12,[1;31]);
N = [2;3];
T = table(D,N)
T{:,1}.Format = 'yyyy-MM-dd' % Using indexing does not work...
T.D.Format = 'yyyy-MM-dd' % but using variable names works!
The same might apply to other object types, e.g. duration.
See Also
Categories
Find more on Data Type Conversion 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!