Filter löschen
Filter löschen

How to extract date, month, year, and time from a table data?

41 Ansichten (letzte 30 Tage)
Abhishek Chakraborty
Abhishek Chakraborty am 19 Dez. 2022
Bearbeitet: Stephen23 am 19 Dez. 2022
I have a table data like "A" like this:
>> A(1:49,1)
ans =
49×1 table
DateTime
___________________
2016-01-01 00:30:00
2016-01-01 01:00:00
2016-01-01 01:30:00
2016-01-01 02:00:00
2016-01-01 02:30:00
2016-01-01 03:00:00
2016-01-01 03:30:00
2016-01-01 04:00:00
2016-01-01 04:30:00
2016-01-01 05:00:00
2016-01-01 05:30:00
2016-01-01 06:00:00
2016-01-01 06:30:00
2016-01-01 07:00:00
2016-01-01 07:30:00
2016-01-01 08:00:00
2016-01-01 08:30:00
2016-01-01 09:00:00
2016-01-01 09:30:00
2016-01-01 10:00:00
2016-01-01 10:30:00
2016-01-01 11:00:00
2016-01-01 11:30:00
2016-01-01 12:00:00
2016-01-01 12:30:00
2016-01-01 13:00:00
2016-01-01 13:30:00
2016-01-01 14:00:00
2016-01-01 14:30:00
2016-01-01 15:00:00
2016-01-01 15:30:00
2016-01-01 16:00:00
2016-01-01 16:30:00
2016-01-01 17:00:00
2016-01-01 17:30:00
2016-01-01 18:00:00
2016-01-01 18:30:00
2016-01-01 19:00:00
2016-01-01 19:30:00
2016-01-01 20:00:00
2016-01-01 20:30:00
2016-01-01 21:00:00
2016-01-01 21:30:00
2016-01-01 22:00:00
2016-01-01 22:30:00
2016-01-01 23:00:00
2016-01-01 23:30:00
2016-01-02 00:00:00
2016-01-02 00:30:00
This is a very large array containing time stamp for every half hour interval for 2 year duration. I wanted to extract the year (say 2016), day (say 1), month (say 1), and time (say 00:30 as 0.5) from this table data. How to do the same? Kindly help me.

Antworten (2)

VBBV
VBBV am 19 Dez. 2022
Bearbeitet: VBBV am 19 Dez. 2022
A = string({'2016-01-09 00:30:00';'2017-05-10 01:00:00'});
At = table(A,'VariableNames',{'Time'});
for k = 1:length(At.Time)
Y(k) = datetime(At.Time(k),'Format','yyyy'); % year
M(k) = datetime(At.Time(k),'Format','MM'); % month
D(k) = datetime(At.Time(k),'Format','dd'); % day
H(k) = datetime(At.Time(k),'Format','hh:mm'); % time
end
Y, M, D , H
Y = 2×1 datetime array
2016 2017
M = 1×2 datetime array
01 05
D = 1×2 datetime array
09 10
H = 1×2 datetime array
12:30 01:00
  1 Kommentar
Stephen23
Stephen23 am 19 Dez. 2022
Bearbeitet: Stephen23 am 19 Dez. 2022
This code simply changes the formatting, but the underlying date/time data is still stored in the DATETIME object. So it does not actually "extract the year ... day .. month ... and time ... from this table data", it just changes how the DATETIME object is displayed.

Melden Sie sich an, um zu kommentieren.


Stephen23
Stephen23 am 19 Dez. 2022
Bearbeitet: Stephen23 am 19 Dez. 2022
The MATLAB approach is to use YMD() and TIMEOFDAY() and HOURS():
S = ["2016-01-01 00:30:00"; "2016-01-01 01:00:00"; "2016-01-01 01:30:00"; "2016-01-01 02:00:00"; "2016-01-01 02:30:00"; "2016-01-01 03:00:00"; "2016-01-01 03:30:00"];
T = datetime(S)
T = 7×1 datetime array
01-Jan-2016 00:30:00 01-Jan-2016 01:00:00 01-Jan-2016 01:30:00 01-Jan-2016 02:00:00 01-Jan-2016 02:30:00 01-Jan-2016 03:00:00 01-Jan-2016 03:30:00
[Y,M,D] = ymd(T)
Y = 7×1
2016 2016 2016 2016 2016 2016 2016
M = 7×1
1 1 1 1 1 1 1
D = 7×1
1 1 1 1 1 1 1
H = hours(timeofday(T))
H = 7×1
0.5000 1.0000 1.5000 2.0000 2.5000 3.0000 3.5000

Kategorien

Mehr zu Dates and Time finden Sie in Help Center und File Exchange

Produkte


Version

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by