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
Stephen23 2018-6-29
编辑:Stephen23 2018-6-29
What you have appears to be a numeric scalar, so that indexing is unrelated. How did that numeric end up in the table in the first place? Probably the easiest solution would be to fix the importing of the file...

请先登录,再进行评论。

采纳的回答

Stephen23
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
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.
Cameron Power
Cameron Power 2018-6-29
That worked perfectly, thank you kindly!

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Time Series Objects 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by