Parsing variable in table

2 visualizaciones (últimos 30 días)
Inna Pelloso
Inna Pelloso el 17 de Nov. de 2022
Comentada: Star Strider el 17 de Nov. de 2022
Hi,
I have a 3x1 table, with one variable, timestamp:
timestamp
2021-02-01 09:00:00
2021-02-01 09:01:00
2021-02-01 09:02:00
How can I parse this to create a 3x2 table, with two variables, date and time:
date time
2021-02-01 09:00:00
2021-02-01 09:01:00
2021-02-01 09:02:00
Any help would be appreciated!
IP
  2 comentarios
Stephen23
Stephen23 el 17 de Nov. de 2022
Editada: Stephen23 el 17 de Nov. de 2022
Note that changing the FORMAT does not changes the actual date/time values stored in the DATETIME object, it only changes how the DATETIME object looks when displayed or converted to text.
If you actually want to have the data consist only of the date and time values separately, then use DATESHIFT and TIMEOFDAY.
Inna Pelloso
Inna Pelloso el 17 de Nov. de 2022
Thank you for your help, Stephen! I really appreciate it!
Inna

Iniciar sesión para comentar.

Respuesta aceptada

Star Strider
Star Strider el 17 de Nov. de 2022
Editada: Star Strider el 17 de Nov. de 2022
Try something like this —
T1 = table(datetime(['2021-02-01 09:00:00'
'2021-02-01 09:01:00'
'2021-02-01 09:02:00']), 'VariableNames',{'timestamp'})
T1 = 3×1 table
timestamp ____________________ 01-Feb-2021 09:00:00 01-Feb-2021 09:01:00 01-Feb-2021 09:02:00
date = T1.timestamp;
time = T1.timestamp;
date.Format = 'yyyy-MM-dd';
time.Format = 'HH:mm:ss';
T1 = addvars(T1,date,time);
T1 = removevars(T1,'timestamp')
T1 = 3×2 table
date time __________ ________ 2021-02-01 09:00:00 2021-02-01 09:01:00 2021-02-01 09:02:00
There may also be other ways to do this.
EDIT — (17 Nov 2022 at 13:16)
I was not certain what you wanted.
Try this —
T1 = table(datetime(['2021-02-01 09:00:00'
'2021-02-01 09:01:00'
'2021-02-01 09:02:00']), 'VariableNames',{'timestamp'})
T1 = 3×1 table
timestamp ____________________ 01-Feb-2021 09:00:00 01-Feb-2021 09:01:00 01-Feb-2021 09:02:00
[y,m,d] = ymd(T1.timestamp); % Year, Month, Day Only
date = datetime([y,m,d]); % Convert Back To 'datetime'
time = timeofday(T1.timestamp); % Time Of Day Only
T1 = addvars(T1,date,time);
T1 = removevars(T1,'timestamp')
T1 = 3×2 table
date time ___________ ________ 01-Feb-2021 09:00:00 01-Feb-2021 09:01:00 01-Feb-2021 09:02:00
.
  4 comentarios
Inna Pelloso
Inna Pelloso el 17 de Nov. de 2022
Many thanks!
Star Strider
Star Strider el 17 de Nov. de 2022
As always, my pleasure!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

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

Etiquetas

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by