date formats

hi,
i have a .txt file containing dates in such a format : Sat Oct 01 20:00:00 BST 2011, where BST stands for British Summer Time. I want to read the file and transform to datenums based on the format: 01-Oct-2011 20:00:00 is there any quick way (in terms of processing as some files contain more than 10000 dates) to do this? Thanks in advance, ~Conrad

 Respuesta aceptada

Daniel Shub
Daniel Shub el 25 de Oct. de 2011

1 voto

Assuming that all the dates are in BST and that you are not trying to adjust for different time zones ...
x = 'Sat Oct 01 20:00:00 BST 2011';
datestr(datenum(x([1:20,25:28]), 'ddd mmm dd HH:MM:SS yyyy'), 'dd-mmm-yyyy HH:MM:SS')

4 comentarios

Daniel Shub
Daniel Shub el 25 de Oct. de 2011
I am sure Jan is going to chime in with a date solution that is 100x faster. It will probably be similar to something in this blog: http://undocumentedmatlab.com/blog/datenum-performance/
Jan
Jan el 12 de Dic. de 2011
@Daniel: I'm so sorry, I've been on holidays in October. What about this:
x = 'Sat Oct 01 20:00:00 BST 2011';
y = x([9:11, 5:8, 25:28, 11:19])
y([3, 7]) = '-';
1000 conversions need 0.03 seconds (Matlab 2009a/64), while the DATENUM-methods needs 1.01 seconds. This is a speedup of only 30 - sorry.
Daniel Shub
Daniel Shub el 12 de Dic. de 2011
I think that is probably the fastest way. I didn't realized that all that was needed was a reorganization of the array.
Jan
Jan el 12 de Dic. de 2011
@Daniel: A lot of performance and confusion problems can be solved by the KISS rule: Keep it simple stupid.
I've learned programming on a ZX81 with 1kB RAM. Neither DATENUM, nor DATESTR, nor the underlying C-mex functions would match in this microscopic box anymore, but the barely dull indexing trick would.
Even an Arduiono feels like a mainframe computer, if you are old enough...

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Productos

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by