Borrar filtros
Borrar filtros

Convert to date, hour, and minute

5 visualizaciones (últimos 30 días)
Bahram
Bahram el 23 de Abr. de 2012
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.

Respuesta aceptada

Andrei Bobrov
Andrei Bobrov el 23 de Abr. de 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 comentarios
Oleg Komarov
Oleg Komarov el 23 de Abr. de 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.
Bahram
Bahram el 23 de Abr. de 2012
Thanks a lot.
I am very new to this community, and I am also fairly new to MATLAB. I'll be more specific in the future!
Thanks agin.

Iniciar sesión para comentar.

Más respuestas (3)

Geoff
Geoff el 23 de Abr. de 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 comentarios
Walter Roberson
Walter Roberson el 23 de Abr. de 2012
Do not quote str in the datenum() call: it is a variable rather than a literal.
Bahram
Bahram el 23 de Abr. de 2012
Thanks for your reply. This correction fixed the time stamp, but the time stamp gets messed up if the original value has less than four digits. I used the following code for the data shown below.
clc
clear
M=csvread('data.dat');
str=[num2str(M(1:end,2)) num2str(M(1:end,4))];
answer=datestr(datenum(str, 'yyyyHHMM')+M(1:end,3), 'mm/dd/yyyy HH:MM');
Data:
221,2012,43,1900,13.63,2.745
221,2012,43,2000,13.39,.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
221,2012,44,100,12.62,-4.951
221,2012,44,200,12.53,-5.684
221,2012,44,300,12.5,-6.337
221,2012,44,400,12.49,-6.93
221,2012,44,500,12.49,-7.52
221,2012,44,600,12.5,-8.01
221,2012,44,700,12.5,-8.42
221,2012,44,800,12.51,-8.75
221,2012,44,900,12.5,-8.97
221,2012,44,1000,12.52,-8.32
Result:
02/13/2012 19:00
02/13/2012 20:00
02/13/2012 21:00
02/13/2012 22:00
02/13/2012 23:00
02/14/0002 01:02
02/14/0201 02:10
02/14/0201 02:20
02/14/0201 02:30
02/14/0201 02:40
02/14/0201 02:50
02/14/0201 03:00
02/14/0201 03:10
02/14/0201 03:20
02/14/0201 03:30
02/14/2012 10:00
Once again, I appreciate your help.

Iniciar sesión para comentar.


Walter Roberson
Walter Roberson el 23 de Abr. de 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 comentarios
Geoff
Geoff el 23 de Abr. de 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
Walter Roberson el 23 de Abr. de 2012
Ah yes, I was worried about floating point round-off when I used round() and didn't think it through.

Iniciar sesión para comentar.


per isakson
per isakson el 23 de Abr. de 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 comentarios
Oleg Komarov
Oleg Komarov el 23 de Abr. de 2012
Expected and documented behavior.
per isakson
per isakson el 23 de Abr. de 2012
I cannot find that documentation!

Iniciar sesión para comentar.

Categorías

Más información sobre Dates and Time en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by