From date and time in different columns to datetime

6 views (last 30 days)
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 Comment
Andrei Bobrov
Andrei Bobrov on 20 Mar 2018
Edited: Andrei Bobrov on 20 Mar 2018
Please attach an example of your data (as mat-file) so that we know what type of data you are using.

Sign in to comment.

Accepted Answer

Akira Agata
Akira Agata on 20 Mar 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 Comments
Felix123
Felix123 on 21 Jul 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.
Peter Perkins
Peter Perkins on 28 Jul 2020
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.

Sign in to comment.

More Answers (1)

Andrei Bobrov
Andrei Bobrov on 20 Mar 2018
Edited: Andrei Bobrov on 20 Mar 2018
datetime('2017-09-29') + hours(21:27)'

Community Treasure Hunt

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

Start Hunting!