Trying to break apart date string
4 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Cameron Power
am 29 Jun. 2018
Kommentiert: Cameron Power
am 29 Jun. 2018
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 Kommentar
Akzeptierte Antwort
Stephen23
am 29 Jun. 2018
>> val = 2.016010100000000e+11;
>> str = sprintf('%u',val)
str = 201601010000
>> vec = datevec(str,'yyyymmddHHMM')
vec =
2016 1 1 0 0 0
3 Kommentare
Stephen23
am 29 Jun. 2018
Bearbeitet: Stephen23
am 29 Jun. 2018
"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.
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Time Series Objects finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!