merge two columns into one

7 views (last 30 days)
swetha S
swetha S on 18 Oct 2019
Answered: Steven Lord on 18 Oct 2019
i have a date column (481*1) and time column (481 *1) separately. How do I merge it into one column in Matlab? Example,
date time
27/8/2019 00:48:20
I want the output as
datetime
27/8/2019 00:48:20
  1 Comment
Andrei Bobrov
Andrei Bobrov on 18 Oct 2019
Attach your variable 'date' and 'time' as mat-file.

Sign in to comment.

Accepted Answer

Steven Lord
Steven Lord on 18 Oct 2019
I'm going to assume you have your data stored as text in a cell array.
dateAndTime = {'27/8/2019','00:48:20'}
Combine the two pieces of the date and time together.
dt = join(dateAndTime)
Looking at your data, I think the right format is to use d (for 1 or 2 digit day of month), M (for 1 or 2 digit month of year), yyyy (4 digit year), HH (24 hour time), mm (minutes), and ss (seconds).
fmt = 'd/M/yyyy HH:mm:ss';
Now convert dt into a datetime array.
dt2 = datetime(dt, 'InputFormat', fmt, 'Format', fmt)
We could skip specifying the InputFormat and datetime looks like it figures out the correct formatting to use for this particular date and time, but if instead of August 27th you wanted to convert August 1st without an InputFormat MATLAB could guess incorrectly (warning you of the ambiguity) and give you a different date and time -- January 8th.
dt3 = {'1/8/2019','00:48:20'}
dt4 = datetime(join(dt3))

More Answers (0)

Categories

Find more on Dates and Time in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!