Convert to date, hour, and minute
Ältere Kommentare anzeigen
Hi,
We collect data from a data logger that collects date and time stamps in addition to the actual data. Here is the date format.
2011 95 1300
The first and third columns are self explanatory. The 2nd column is the no. of days since January the 1st.
I would like to know if there is a way to convert the information into 4/5/2011 13:00.
Thanks in advance.
Akzeptierte Antwort
Weitere Antworten (3)
Geoff
am 23 Apr. 2012
Yep,
First, concatenate the first and third columns to use the normal date conversion, then add the second column. Put the result into datestr:
datestr(datenum('20121300', 'yyyyHHMM')+95, 'mm/dd/yyyy HH:MM')
ans =
04/05/2012 13:00
Obviously I've just used constants here, but you get the idea.
You can't get single-character days out of that though - they are padded with zeros.
In that case you'd want to do this:
d = datevec(datenum('20121300', 'yyyyHHMM')+95, 'mm/dd/yyyy HH:MM');
sprintf( '%d/%d/%d %d:%02d', d([2 3 1 4 5]) )
ans =
4/5/2012 13:00
3 Kommentare
Bahram
am 23 Apr. 2012
Walter Roberson
am 23 Apr. 2012
Do not quote str in the datenum() call: it is a variable rather than a literal.
Bahram
am 23 Apr. 2012
Walter Roberson
am 23 Apr. 2012
Try
datestr([M(:,1), ones(size(M,1),1), M(:,2)+1, round(M(:,3)./100), mod(M(:,3),100), zeros(size(M,1),1)], 'm/d/yyyy HH:MM')
This assumes that "days since January the 1st" will be 0 for Jan 1.
The part in [] constructs dates in the medium-length datevec format (year month day hours minutes seconds). The adjustment to convert between day number and proper month/day is handled by specifying (e.g.) Jan 95th and letting the date calculation routines do the fixup.
2 Kommentare
Geoff
am 23 Apr. 2012
That call to 'round' should be 'floor' or it will give incorrect results for the last 10 minutes of each hour. Unfortunately a single 'm' and 'd' in calls to 'datestr' expand to the capitalised first letter of the month and day respectively.
Walter Roberson
am 23 Apr. 2012
Ah yes, I was worried about floating point round-off when I used round() and didn't think it through.
per isakson
am 23 Apr. 2012
I run this. Matlab seems to more clever than I anticipated. Is this documented behavior?
str = '2011 95 1300'
datestr( datenum( str, 'yyyy dd HHMM' ), 31 )
str = '2012 95 1300'
datestr( datenum( str, 'yyyy dd HHMM' ), 31 )
str =
2011 95 1300
ans =
2011-04-05 13:00:00
str =
2012 95 1300
ans =
2012-04-04 13:00:00
2 Kommentare
Oleg Komarov
am 23 Apr. 2012
Expected and documented behavior.
per isakson
am 23 Apr. 2012
I cannot find that documentation!
Kategorien
Mehr zu Time Series Objects finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!