Azzera filtri
Azzera filtri

Trying to break apart date string

4 visualizzazioni (ultimi 30 giorni)
Cameron Power
Cameron Power il 29 Giu 2018
Commentato: Cameron Power il 29 Giu 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 Commento
Stephen23
Stephen23 il 29 Giu 2018
Modificato: Stephen23 il 29 Giu 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...

Accedi per commentare.

Risposta accettata

Stephen23
Stephen23 il 29 Giu 2018
>> val = 2.016010100000000e+11;
>> str = sprintf('%u',val)
str = 201601010000
>> vec = datevec(str,'yyyymmddHHMM')
vec =
2016 1 1 0 0 0
  3 Commenti
Stephen23
Stephen23 il 29 Giu 2018
Modificato: Stephen23 il 29 Giu 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 il 29 Giu 2018
That worked perfectly, thank you kindly!

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Time Series Objects in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by