Effacer les filtres
Effacer les filtres

Trying to break apart date string

4 vues (au cours des 30 derniers jours)
Cameron Power
Cameron Power le 29 Juin 2018
Commenté : Cameron Power le 29 Juin 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 commentaire
Stephen23
Stephen23 le 29 Juin 2018
Modifié(e) : Stephen23 le 29 Juin 2018
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...

Connectez-vous pour commenter.

Réponse acceptée

Stephen23
Stephen23 le 29 Juin 2018
>> val = 2.016010100000000e+11;
>> str = sprintf('%u',val)
str = 201601010000
>> vec = datevec(str,'yyyymmddHHMM')
vec =
2016 1 1 0 0 0
  3 commentaires
Stephen23
Stephen23 le 29 Juin 2018
Modifié(e) : Stephen23 le 29 Juin 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.
Cameron Power
Cameron Power le 29 Juin 2018
That worked perfectly, thank you kindly!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Time Series Objects dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by