Convert time in decimal days into hh:mm:ss format
Mostra commenti meno recenti
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'
or
D = datetime(N,'ConvertFrom','excel');
T = timeofday(D)
11 Commenti
Zach Morag
il 5 Dic 2021
Zach Morag
il 5 Dic 2021
Steven Lord
il 5 Dic 2021
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
Zach Morag
il 6 Dic 2021
"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
Zach Morag
il 6 Dic 2021
Walter Roberson
il 6 Dic 2021
Modificato: Walter Roberson
il 6 Dic 2021
scaleLog{:,2} = days(scaleLog{:,2});
scaleLog{:,2}.Format = "HH:mm:ss";
Zach Morag
il 6 Dic 2021
Walter Roberson
il 6 Dic 2021
scaleLog.('Time [hh:mm:ss]') = days(scaleLog{:,2});
scaleLog{:,2}.Format = "HH:mm:ss";
Stephen23
il 6 Dic 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?
@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.
please check my profile I made a script very usefull to solve that problem, click here :
1 Commento
Categorie
Scopri di più su Spreadsheets in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
