From date and time in different columns to datetime

Hey, I have an array with date (yyyy-mm-dd) in one columns and time (hh:mm:ss) in another like this:
2017-09-29 21:00:00
2017-09-29 22:00:00
2017-09-29 23:00:00
2017-09-30 00:00:00
2017-09-30 01:00:00
2017-09-30 02:00:00
2017-09-30 03:00:00
How can I make this array into a datetime array?

1 comentario

Andrei Bobrov
Andrei Bobrov el 20 de Mzo. de 2018
Editada: Andrei Bobrov el 20 de Mzo. de 2018
Please attach an example of your data (as mat-file) so that we know what type of data you are using.

Iniciar sesión para comentar.

 Respuesta aceptada

Akira Agata
Akira Agata el 20 de Mzo. de 2018
Assuming your data is stored in CSV file like the attached, readtable function automatically recognize that 1st and 2nd column are datetime and duration. So, the following can make the datetime vector you want.
T = readtable('data1.csv');
time = T{:,1}+T{:,2};

7 comentarios

Espen Mikkelsen
Espen Mikkelsen el 22 de Mzo. de 2018
Editada: Espen Mikkelsen el 22 de Mzo. de 2018
I have tried, but I recieved following error message: "Addition is not defined between datetime arrays".
Akira Agata
Akira Agata el 22 de Mzo. de 2018
What is your MATLAB version? At least the latest one (R2018a), this code works. I would recommend checking the type of 1st and 2nd column of T. They should be datetime and duration, respectively.
This will only work for R2018a. Prior to that, do this:
T.date = T.date + timeofday(T.time)
This assumes your CSV has column headers "date" and "time".
I am now trying this and I get the error:
"Addition is not defined between cell and duration arrays."
I'm using R 2018B and loaded my data using readtable.
Can anyone help? What does a cell array mean? And how do I change it? Thank you
dd = 'input_data';
nowd = cd;
d = dir('*.csv');
for j=1:length(d)
tic
filename=d(j).name;
disp(filename);
dat=readtable(filename);
%data=table2timetable(dat, 'RowTimes', 'LOCALTIME'); %orientate timetable using 'localtime' as the time vector
try
fid = fopen(fullfile(dd,filename));
data=removevars(dat, [1 2 3 4 7 9 10]); %remove columns I am not interested in
time=data{:,1}+data{:,2};
catch
disp('error');
fclose(filename);
end
end
Peter Perkins
Peter Perkins el 9 de Mayo de 2019
Editada: Peter Perkins el 9 de Mayo de 2019
Thsi seems to be in the wrong thread.
"cell array" means that for some reason, readtable believes that the colummn in your CSV that you want to treat as dates, is text. Not sure why that is, but the solution is to use detectimportoptions and then adjust things to tell it that that column is in fact dates.
Felix123
Felix123 el 21 de Jul. de 2020
@Peter Perking: can you be more specific on how to adjust these. I have not ever had a spreadsheet, where this was figured out automatically.
It's described in the examples here:
If a recent version of readtable (NOT xlsread) can't automatically identify date columns, then there's something in the spreadsheet that's not "right". The cells in the spreadsheet may not be formatted as dates. There may be a mix of text and other things. Hard to say for sure, but recent versions of readtable are pretty good at detecting dates.

Iniciar sesión para comentar.

Más respuestas (1)

Andrei Bobrov
Andrei Bobrov el 20 de Mzo. de 2018
Editada: Andrei Bobrov el 20 de Mzo. de 2018
datetime('2017-09-29') + hours(21:27)'

Categorías

Preguntada:

el 20 de Mzo. de 2018

Comentada:

el 28 de Jul. de 2020

Community Treasure Hunt

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

Start Hunting!

Translated by