merge two columns into one

7 vues (au cours des 30 derniers jours)
swetha S
swetha S le 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 commentaire
Andrei Bobrov
Andrei Bobrov le 18 Oct 2019
Attach your variable 'date' and 'time' as mat-file.

Connectez-vous pour commenter.

Réponse acceptée

Steven Lord
Steven Lord le 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))

Plus de réponses (0)

Catégories

En savoir plus sur Dates and Time dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by