Trying to break apart date string
조회 수: 4 (최근 30일)
이전 댓글 표시
I have a table with a column that has a large string for the date, I want to isolate certain digits and separate them for example if I have date = 2.016010100000000e+11 I want yyyy = date(1:4) MM = date(5:6) dd = date(7:8) HH = date(9:12)
The only problem is that I want to do this for the entire table, is there a way to? Thank you
댓글 수: 1
채택된 답변
Stephen23
2018년 6월 29일
>> val = 2.016010100000000e+11;
>> str = sprintf('%u',val)
str = 201601010000
>> vec = datevec(str,'yyyymmddHHMM')
vec =
2016 1 1 0 0 0
댓글 수: 3
Stephen23
2018년 6월 29일
편집: Stephen23
2018년 6월 29일
"when it was recorded no delimeters were used in the dating,..."
That doesn't matter, we can handle that!
"...is there a way to implement this to fix every date in the table(every date is in this exact format)?"
One option would be to import the data properly to start with, using either readtable or textscan. Both of these have formats for reading dates, which is explained in their help, so I will not bore you with that here.
As an alternative you could simply import as numeric (e.g. using csvread), convert the first column to a character matrix, and then use datevec:
>> mat = csvread('aaab-dbase-2016-04-21.csv');
>> chr = num2str(mat(:,1));
>> chr(1:10,:)
ans =
201511180800
201511180810
201511180820
201511180830
201511180840
201511180850
201511180900
201511180910
201511180920
201511180930
>> dtv = datevec(chr,'yyyymmddHHMM');
>> dtv(1:10,:)
ans =
2015 11 18 8 0 0
2015 11 18 8 10 0
2015 11 18 8 20 0
2015 11 18 8 30 0
2015 11 18 8 40 0
2015 11 18 8 50 0
2015 11 18 9 0 0
2015 11 18 9 10 0
2015 11 18 9 20 0
2015 11 18 9 30 0
dtv is a numeric matrix: the first column is the year, the second column the month, etc.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Time Series Objects에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!