merge two columns into one

7 visualizaciones (últimos 30 días)
swetha S
swetha S el 18 de Oct. de 2019
Respondida: Steven Lord el 18 de Oct. de 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 comentario
Andrei Bobrov
Andrei Bobrov el 18 de Oct. de 2019
Attach your variable 'date' and 'time' as mat-file.

Iniciar sesión para comentar.

Respuesta aceptada

Steven Lord
Steven Lord el 18 de Oct. de 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))

Más respuestas (0)

Categorías

Más información sobre Dates and Time en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by