time date to datenum and datenum to datetime

I have time in 'yyyy-MM-dd''T''HH:mm:ss.SSS''Z' formate (e.g., [2018-08-01T20:05:00.000Z; 2018-08-01T20:10:00.000Z; 2018-08-01T20:35:00.000Z; 2018-08-01T25:05:00.000Z] . How to make time string and datetime to datenum and datenum to datetime?
Thanks in advance.
(I am new in matlab.)

5 comentarios

Stephen23
Stephen23 el 26 de Abr. de 2022
Editada: Stephen23 el 26 de Abr. de 2022
What is the particular need to use serial date numbers?
In general you should avoid deprecated serial date numbers and use DATETIME/DURATION-based code.
Ismita
Ismita el 26 de Abr. de 2022
But I need it serially here
Jan
Jan el 26 de Abr. de 2022
25:05 is a strange time.
Please post some valid Matlab code, which creates exactly the input you have. "[2018-08-01T20:05:00.000Z; 2018-08-01T20:10:00.000Z; 2018-08-01T20:35:00.000Z; 2018-08-01T25:05:00.000Z]" is no valid code, so the readers must invent your data at first and this is fragile.
Stephen23
Stephen23 el 26 de Abr. de 2022
Editada: Stephen23 el 26 de Abr. de 2022
"But I need it serially here"
What is the exact operation that requires a serial date number?
Both DATETIME and DURATION objects support many many methods and functions, if you actually explained how you are going to process your data then we could help you use a better approach.
Ismita
Ismita el 26 de Abr. de 2022
Thank you. You can check the data attached in the comment threading.

Iniciar sesión para comentar.

 Respuesta aceptada

Bruno Luong
Bruno Luong el 26 de Abr. de 2022
Editada: Bruno Luong el 26 de Abr. de 2022
I make a round conversion so you can convert any format to any other by using the appropriate substeps
dt=datetime(2022,04,28,13,09,00,'Format','yyyy-MM-dd''T''HH:mm:ss.SSS''Z''')
dt = datetime
2022-04-28T13:09:00.000Z
dn=datenum(dt)
dn = 7.3864e+05
ds = datestr(dn)
ds = '28-Apr-2022 13:09:00'
dt = datetime(ds,'InputFormat','dd-MMM-yyyy HH:mm:SS','Format','yyyy-MM-dd''T''HH:mm:ss.SSS''Z''')
dt = datetime
2022-04-28T13:09:00.000Z

11 comentarios

Ismita
Ismita el 26 de Abr. de 2022
Thank you. How can I make such a column like dt? Can I use split/ something to make the column?
Bruno Luong
Bruno Luong el 26 de Abr. de 2022
I don't understand. A column of what? from what? What is "split/ something"?
Jan
Jan el 26 de Abr. de 2022
@Mst Ismita Tasnim: I've asked you already, what exactly your input arguments are. Please post, how your data look.
Ismita
Ismita el 26 de Abr. de 2022
Editada: Ismita el 26 de Abr. de 2022
Thank you so much. sorry for being late. Time is "'yyyy-MM-dd''T''HH:mm:ss.SSS''Z'", e.g.,
2008-01-01T00:00:00.000Z -1.06
2008-01-01T00:05:00.000Z -1
2008-01-01T00:10:00.000Z -1.76
2008-01-01T00:15:00.000Z -2.09
2008-01-01T00:20:00.000Z -1.74
2008-01-01T00:25:00.000Z 999
2008-01-01T00:30:00.000Z -3.64
2008-01-01T00:35:00.000Z -3.65
2008-01-01T00:40:00.000Z 999
2008-01-01T00:45:00.000Z -3.68
2008-01-01T00:50:00.000Z -3.98
2008-01-01T00:55:00.000Z -3.83
2008-01-01T01:00:00.000Z -3.48
2008-01-01T01:05:00.000Z -3.45
2008-01-01T01:10:00.000Z -3.24
2008-01-01T01:15:00.000Z -2.4
Bruno Luong
Bruno Luong el 26 de Abr. de 2022
@Mst Ismita Tasnim Sigh..., What are these data? Do you copy from MATLAB command line? You excel file? Your notebook? Do you hire someone to type in the computer for you?
If it's come from MATLAB data, please explain us the type/class/size, or save it as mat file and attach here.
What do you want to do with it?
PLEASE EXPLAIN!
Bruno Luong
Bruno Luong el 26 de Abr. de 2022
Editada: Bruno Luong el 26 de Abr. de 2022
In the mean time you explain, here is an exemple with vector of datetime
dt=datetime(2022,04,28,12+randi(12,5,1),09,00,'Format','yyyy-MM-dd''T''HH:mm:ss.SSS''Z''')
dt = 5×1 datetime array
2022-04-28T17:09:00.000Z 2022-04-28T23:09:00.000Z 2022-04-28T20:09:00.000Z 2022-04-28T17:09:00.000Z 2022-04-29T00:09:00.000Z
dn=datenum(dt)
dn = 5×1
1.0e+05 * 7.3864 7.3864 7.3864 7.3864 7.3864
ds = datestr(dn)
ds = 5×20 char array
'28-Apr-2022 17:09:00' '28-Apr-2022 23:09:00' '28-Apr-2022 20:09:00' '28-Apr-2022 17:09:00' '29-Apr-2022 00:09:00'
dt = datetime(ds,'InputFormat','dd-MMM-yyyy HH:mm:SS','Format','yyyy-MM-dd''T''HH:mm:ss.SSS''Z''')
dt = 5×1 datetime array
2022-04-28T17:09:00.000Z 2022-04-28T23:09:00.000Z 2022-04-28T20:09:00.000Z 2022-04-28T17:09:00.000Z 2022-04-29T00:09:00.000Z
Ismita
Ismita el 26 de Abr. de 2022
I have attached the txt file that I build from csv data set.
Bruno Luong
Bruno Luong el 26 de Abr. de 2022
Editada: Bruno Luong el 26 de Abr. de 2022
Thanks goodness finally a data file.
Import and convert to datetime as below, do the rest as with my earlier answer
s=readtable('File.txt')
s = 10×10 table
Var1 Var2 Var3 Var4 Var5 Var6 Var7 Var8 Var9 Var10 ____________________________ _____ _____ _____ ______ ____ _____ ____ _____ _____ {'2008-01-01T00:00:00.000Z'} -1.06 4.3 1.2 -355.9 -9 -16.6 6.58 66335 0 {'2008-01-01T00:05:00.000Z'} -1 3.89 1.26 -353.4 -6.7 -18 6.6 71037 300 {'2008-01-01T00:10:00.000Z'} -1.76 3.22 -0.7 -350 0.6 -11.9 6.59 74186 600 {'2008-01-01T00:15:00.000Z'} -2.09 3.29 -0.49 -349.5 -2 -4.4 6.62 88787 900 {'2008-01-01T00:20:00.000Z'} -1.74 2.3 -1.49 -351.4 -2.6 -3.7 6.65 82061 1200 {'2008-01-01T00:25:00.000Z'} -3.27 0.41 -2.61 -346.2 2.8 -0.3 6.47 74540 1500 {'2008-01-01T00:30:00.000Z'} -3.64 -0.44 -2.43 -343.7 8.4 3.5 6.09 82033 1800 {'2008-01-01T00:35:00.000Z'} -3.65 0.27 -1.81 -343.2 12.4 0 6.35 82179 2100 {'2008-01-01T00:40:00.000Z'} -3.4 1.72 -1.67 -341.8 5.7 5.2 6.15 77514 2400 {'2008-01-01T00:45:00.000Z'} -3.68 0.59 -1.39 -343.1 0.5 -4.7 6.27 80708 2700
ts=table2cell(s(:,1))
ts = 10×1 cell array
{'2008-01-01T00:00:00.000Z'} {'2008-01-01T00:05:00.000Z'} {'2008-01-01T00:10:00.000Z'} {'2008-01-01T00:15:00.000Z'} {'2008-01-01T00:20:00.000Z'} {'2008-01-01T00:25:00.000Z'} {'2008-01-01T00:30:00.000Z'} {'2008-01-01T00:35:00.000Z'} {'2008-01-01T00:40:00.000Z'} {'2008-01-01T00:45:00.000Z'}
dtformat = 'yyyy-MM-dd''T''HH:mm:ss.SSS''Z';
dt = datetime(ts,'Format',dtformat,'InputFormat',dtformat)
dt = 10×1 datetime array
2008-01-01T00:00:00.000Z 2008-01-01T00:05:00.000Z 2008-01-01T00:10:00.000Z 2008-01-01T00:15:00.000Z 2008-01-01T00:20:00.000Z 2008-01-01T00:25:00.000Z 2008-01-01T00:30:00.000Z 2008-01-01T00:35:00.000Z 2008-01-01T00:40:00.000Z 2008-01-01T00:45:00.000Z
Ismita
Ismita el 26 de Abr. de 2022
Editada: Ismita el 26 de Abr. de 2022
Thank you so much. If I use 's=readtable('File.txt')', do Var2, Var3, Var4,.... behave like text/string? I mean do I need to use anything like Var2 = str2double(s(:,2:9)) or 'table2array(s(:,2:9))' ?
Bruno Luong
Bruno Luong el 26 de Abr. de 2022
Editada: Bruno Luong el 26 de Abr. de 2022
They are table, if you aceess them by column name, it returns the standard column-vector of doubles
s.Var2 + s.Var3
Ismita
Ismita el 26 de Abr. de 2022
thanks a lot.

Iniciar sesión para comentar.

Más respuestas (2)

I'm going to assume that you've imported this data into MATLAB as a string array or a cellstr with two columns. Since I don't have the file or whatever other source this data comes from, I'll hard code it so my example can operate on it.
S = ["2008-01-01T00:00:00.000Z", "-1.06";
"2008-01-01T00:05:00.000Z", "-1";
"2008-01-01T00:10:00.000Z", "-1.76";
"2008-01-01T00:15:00.000Z", "-2.09"];
Let's create a datetime array using the first column of S.
fmt = 'yyyy-MM-dd''T''HH:mm:ss.SSS''Z''';
dt = datetime(S(:, 1), 'InputFormat', fmt, 'Format', fmt)
dt = 4×1 datetime array
2008-01-01T00:00:00.000Z 2008-01-01T00:05:00.000Z 2008-01-01T00:10:00.000Z 2008-01-01T00:15:00.000Z
Now to create a double array using the second column of S.
n = double(S(:, 2))
n = 4×1
-1.0600 -1.0000 -1.7600 -2.0900
Finally, storing this date and time data along with the numeric data in a timetable array would let us perform future operations on the data easily.
T = timetable(dt, n)
T = 4×1 timetable
dt n ________________________ _____ 2008-01-01T00:00:00.000Z -1.06 2008-01-01T00:05:00.000Z -1 2008-01-01T00:10:00.000Z -1.76 2008-01-01T00:15:00.000Z -2.09

1 comentario

Ismita
Ismita el 26 de Abr. de 2022
Editada: Ismita el 26 de Abr. de 2022
Thank you so much. Could you please inform me the type of 'T=timetable(dt,n)' data is text/numeric?
Should I use 'double(n)' / 'table2array(n)' for other variables like 'n' here?

Iniciar sesión para comentar.

Corey Silva
Corey Silva el 18 de Mayo de 2022

0 votos

While serial date numbers (datenum) can represent dates and times, it is recommended that you use datetime values to represent points in time, and durationor calendarDuration values to represent elapsed times.

Categorías

Preguntada:

el 26 de Abr. de 2022

Respondida:

el 18 de Mayo de 2022

Community Treasure Hunt

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

Start Hunting!

Translated by