Convert to date, hour, and minute
5 views (last 30 days)
Show older comments
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.
0 Comments
Accepted Answer
Andrei Bobrov
on 23 Apr 2012
added after the receipt of Bahram's data [edited]
d = [ 221 2012 43 1900 13.63 2.745
221 2012 43 2000 13.39 0.002
221 2012 43 2100 13.2 -2.362
221 2012 43 2200 13.04 -3.703
221 2012 43 2300 12.87 -4.243
221 2012 44 0 12.73 -4.555]
d1 = d(:,4)/100;
out = datestr(datenum(d(:,2), 1, d(:,3), fix(d1), 100*rem(d1,1), 0))
6 Comments
Oleg Komarov
on 23 Apr 2012
Then you don't need to convert your dates to strings:
out = datenum(d(:,2), 1, d(:,3), fix(d1), 100*rem(d1,1), 0);
The appearance on the plot is controlled with datetick:
plot(out, d(:,5))
datetick('x','dd-mmm-yyyy HH:MM')
Where you can customize the apperance by chosing the appropriate format. You can find the details in the documentation of datetick, datenum, datestr etc..
A note for future questions: specify what you need your task for. If you asked from the beginning that you wanted to use it in a plot, the answer could have been much more straightforward.
More Answers (3)
Geoff
on 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 Comments
Walter Roberson
on 23 Apr 2012
Do not quote str in the datenum() call: it is a variable rather than a literal.
Walter Roberson
on 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 Comments
Geoff
on 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
on 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
on 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 Comments
See Also
Categories
Find more on Dates and Time in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!