필터 지우기
필터 지우기

Creating a datetime variable - unconventionally

조회 수: 1 (최근 30일)
Lewis Waswa
Lewis Waswa 2022년 3월 17일
댓글: Star Strider 2022년 3월 18일
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.

채택된 답변

Star Strider
Star Strider 2022년 3월 17일
Using months instead of minutes —
Date = {'200301'; '200306'; '200312'}
Date = 3×1 cell array
{'200301'} {'200306'} {'200312'}
DateDT = datetime(Date, 'InputFormat','yyyyMM')
DateDT = 3×1 datetime array
01-Jan-2003 01-Jun-2003 01-Dec-2003
DateDT.Format = 'yyyyMM'
DateDT = 3×1 datetime array
200301 200306 200312
.
  댓글 수: 6
Lewis Waswa
Lewis Waswa 2022년 3월 18일
Thank you @Star Strider. This works well.
Star Strider
Star Strider 2022년 3월 18일
As always, my pleasure!

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Arif Hoq
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')
date2 = datetime
2003-01
then every 48 interval try this
T.Date(1:48:end,:) % if T is your table

Arif Hoq
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
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 CenterFile Exchange에서 Dates and Time에 대해 자세히 알아보기

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by