Convert time in decimal days into hh:mm:ss format

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?

Risposte (2)

N = 0.504513888888889;
T = days(N);
T.Format = 'hh:mm:ss'
T = duration
12:06:30
or
D = datetime(N,'ConvertFrom','excel');
T = timeofday(D)
T = duration
12:06:30

11 Commenti

Thx for your answer by it still doesn't work
It might be related that the time log is column that converted to table array?
Error using days (line 26)
Input data must be a real, numeric array, or a duration array.
Error using datetime (line 586)
Input data must be one numeric matrix when converting from a different date/time representation.
Let's make sure you're using the days function included with MATLAB. What does this command show?
which -all days
And just for completeness, let's do the same for datetime.
which -all datetime
Both functions are from MATLAB directory
I think the problem is that the varlibles that I want to change, are table type
and the matlab doesn't let me to change them
"I think the problem is that the varlibles that I want to change, are table type and the matlab doesn't let me to change them"
Most likely you do not need to "change" them. Tables are a container class (much like cell arrays and structures) which simply contain arrays of other types. So inside the table you can have numeric data, datetime data, etc.
You can easily access the data in a table using the table variable/column names, indexing, etc:
For example, if the datetime data is in a table variable/column named "date" then you could simply replace N in my answer with
yourtable.date
this is the code im using:
scaleLog(:,2)=days(scaleLog(:,2));
scaleLog(:,2).Format = "HH:mm:ss";
the second column contains the varlibles of time in decimal valurs
but still it deosn't work
the table:
The error message:
Error using days (line 26)
Input data must be a real, numeric array, or a duration array.
scaleLog{:,2} = days(scaleLog{:,2});
scaleLog{:,2}.Format = "HH:mm:ss";
thx its seems like we have a progress but stil I got the following message:
Unable to perform assignment because value of type 'duration' is not convertible to 'double'.
Caused by:
Error using duration/double (line 1075)
Undefined function 'double' for input arguments of type 'duration'. To convert from durations to numeric, use the SECONDS, MINUTES, HOURS, DAYS, or YEARS functions.
should I cahnge the couln of the table to double type? what is the command for it?
scaleLog.('Time [hh:mm:ss]') = days(scaleLog{:,2});
scaleLog{:,2}.Format = "HH:mm:ss";
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?
@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 = 2×2 table
D N ___________ _ 01-Dec-2021 2 31-Dec-2021 3
T{:,1}.Format = 'yyyy-MM-dd' % Using indexing does not work...
T = 2×2 table
D N ___________ _ 01-Dec-2021 2 31-Dec-2021 3
T.D.Format = 'yyyy-MM-dd' % but using variable names works!
T = 2×2 table
D N __________ _ 2021-12-01 2 2021-12-31 3
The same might apply to other object types, e.g. duration.

Accedi per commentare.

TAMRABET
TAMRABET il 27 Gen 2023
Modificato: TAMRABET il 27 Gen 2023
please check my profile I made a script very usefull to solve that problem, click here :

Prodotti

Release

R2021a

Richiesto:

il 5 Dic 2021

Modificato:

il 27 Gen 2023

Community Treasure Hunt

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

Start Hunting!

Translated by