Creating a datetime variable - unconventionally
1 次查看(过去 30 天)
显示 更早的评论
I have an excel sheet which has several columns including a date column of which I would like to create a datetime object from. The snippet of this data is as shown below.
I would like to create a datetime object such that shows the year and the month alone ( in this case 2003-January) with 48 intervals for each month - for the whole range of the year. It is more of replicating the 2003-January datetime 48 times and then moving next to 2003-February till when we reach December. Any help will be appreciated.
0 个评论
采纳的回答
Star Strider
2022-3-17
Using months instead of minutes —
Date = {'200301'; '200306'; '200312'}
DateDT = datetime(Date, 'InputFormat','yyyyMM')
DateDT.Format = 'yyyyMM'
.
6 个评论
更多回答(2 个)
Arif Hoq
2022-3-17
As you did not attach your data. let's try with your first data
A='200301';
date=datetime(A,'InputFormat','yyyymm');
date2=datetime(date,'Format','yyyy-mm')
then every 48 interval try this
T.Date(1:48:end,:) % if T is your table
Arif Hoq
2022-3-17
try this:
B=readtable('Data-E.xlsx');
C=B(1:48:end,:);
dat=string(table2cell(C(:,1)));
date = datetime(dat, 'InputFormat','yyyyMM');
date2=datetime(date,'Format','MMM-yyyy');
date3=rmmissing(date2);
1 个评论
Arif Hoq
2022-3-17
or try this:
B=readtable('Data-E.xlsx');
C=B(1:48:end,:);
dat=string(table2cell(C(:,1)));
date = datetime(dat, 'InputFormat','yyyyMM');
date2=datetime(date,'Format','MMM-yyyy');
% [R,TF]=rmmissing(date2)
D=cellstr(date2)
[D Lia Lib]=unique(D,'rows','stable')
E=C(Lia,:)
E(13,:)=[] % deleting Nat value
value=string(table2cell(E(:,2:end)));
dat2=string(table2cell(E(:,1)));
date3 = datetime(dat2, 'InputFormat','yyyyMM');
date4=datetime(date3,'Format','MMM-yyyy');
T=timetable(date4,value) % final array
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Dates and Time 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!