How to change a variable to a time variable?
Show older comments
I have the following excel file (ejemplo.xlsx) where the first column is date (format yyyy-MM-dd) , the second start hour and the third is last hour. How can I tell Matlab that the second and third columns are datetime variables? I tried the following code but is not working.
Ej = readtable('Ejemplo.xlsx');
ej = table2timetable(Ej);
ej= datetime(ej.StartTime,'Format','HH:mm')
Thanks in advance!
Accepted Answer
More Answers (1)
Steven Lord
on 8 Sep 2020
Those columns in your spreadsheet are not what I would call a datetime. They have a time component, yes, but not a date component. I'd read them in as duration arrays and add them to the datetime from the first column to get a date-and-time.
>> t = detectImportOptions(thefile);
>> t.VariableTypes{2} = 'duration';
>> t.VariableTypes{3} = 'duration';
>> E1 = readtable(thefile, t);
>> head(E1)
ans =
8×5 table
Date StartTime EndTime ForecastInMW ActualInMW
___________ _________ ________ ____________ __________
01-Sep-2010 0 sec 900 sec 1037.6 983.3
01-Sep-2010 900 sec 1800 sec 1071.2 1177.7
01-Sep-2010 1800 sec 2700 sec 1098.5 1397.7
01-Sep-2010 2700 sec 3600 sec 1339.1 1414.4
01-Sep-2010 3600 sec 4500 sec 1360.4 1585.1
01-Sep-2010 4500 sec 5400 sec 1386.7 1811.7
01-Sep-2010 5400 sec 6300 sec 1408.7 1969.5
01-Sep-2010 6300 sec 7200 sec 1531.2 1810.3
>> y = E1.Date(1:5) + E1.StartTime(1:5)
y =
5×1 datetime array
01-Sep-2010 00:00:00
01-Sep-2010 00:15:00
01-Sep-2010 00:30:00
01-Sep-2010 00:45:00
01-Sep-2010 01:00:00
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!