merge two columns into one

7 次查看(过去 30 天)
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

采纳的回答

Steven Lord
Steven Lord 2019-10-18
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))

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Dates and Time 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by