How to change a variable to a time variable?
26 次查看(过去 30 天)
显示 更早的评论
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!
2 个评论
Dana
2020-9-8
"It's not working" is about the vaguest description of a problem I can conceive of. If you put some more effort into your question you might be able to get a useful answer.
采纳的回答
Star Strider
2020-9-8
编辑:Star Strider
2020-9-8
One approach:
Ej = readtable('Ejemplo.xlsx');
Ej.StartTime = Ej{:,1} + days(Ej{:,2});
Ej.EndTime = Ej{:,1} + days(Ej{:,3});
with:
FirstFiveRows = Ej(1:5,:)
producing:
FirstFiveRows =
5×5 table
Date StartTime EndTime ForecastInMW ActualInMW
___________ ____________________ ____________________ ____________ __________
01-Sep-2010 01-Sep-2010 00:00:00 01-Sep-2010 00:15:00 1037.6 983.3
01-Sep-2010 01-Sep-2010 00:15:00 01-Sep-2010 00:30:00 1071.2 1177.7
01-Sep-2010 01-Sep-2010 00:30:00 01-Sep-2010 00:45:00 1098.5 1397.7
01-Sep-2010 01-Sep-2010 00:45:00 01-Sep-2010 01:00:00 1339.1 1414.4
01-Sep-2010 01-Sep-2010 01:00:00 01-Sep-2010 01:15:00 1360.4 1585.1
EDIT — (8 Sep 2020 at 16:02)
To use my code with 'PreserveVariableNames',true (and avoiding the Warning that appears otherwise), my code changes slightly to:
Ej = readtable('Ejemplo.xlsx', 'PreserveVariableNames',1);
Ej.('Start Time') = Ej{:,1} + days(Ej{:,2});
Ej.('End Time') = Ej{:,1} + days(Ej{:,3});
with:
FirstFiveRows = Ej(1:5,:)
producing:
FirstFiveRows =
5×5 table
Date Start Time End Time Forecast in MW Actual in MW
___________ ____________________ ____________________ ______________ ____________
01-Sep-2010 01-Sep-2010 00:00:00 01-Sep-2010 00:15:00 1037.6 983.3
01-Sep-2010 01-Sep-2010 00:15:00 01-Sep-2010 00:30:00 1071.2 1177.7
01-Sep-2010 01-Sep-2010 00:30:00 01-Sep-2010 00:45:00 1098.5 1397.7
01-Sep-2010 01-Sep-2010 00:45:00 01-Sep-2010 01:00:00 1339.1 1414.4
01-Sep-2010 01-Sep-2010 01:00:00 01-Sep-2010 01:15:00 1360.4 1585.1
.
0 个评论
更多回答(1 个)
Steven Lord
2020-9-8
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
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Calendar 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!