Convert to date, hour, and minute
이전 댓글 표시
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.
채택된 답변
추가 답변 (3개)
Geoff
2012년 4월 23일
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
Bahram
2012년 4월 23일
Walter Roberson
2012년 4월 23일
Do not quote str in the datenum() call: it is a variable rather than a literal.
Bahram
2012년 4월 23일
Walter Roberson
2012년 4월 23일
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
Geoff
2012년 4월 23일
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
2012년 4월 23일
Ah yes, I was worried about floating point round-off when I used round() and didn't think it through.
per isakson
2012년 4월 23일
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
Oleg Komarov
2012년 4월 23일
Expected and documented behavior.
per isakson
2012년 4월 23일
I cannot find that documentation!
카테고리
도움말 센터 및 File Exchange에서 Calendar에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!