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

 采纳的回答

Daniel Shub
Daniel Shub 2011-10-25
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 个评论

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 2011-12-12
@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.
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 2011-12-12
@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...

请先登录,再进行评论。

更多回答(0 个)

类别

在 帮助中心 和 File Exchange 中查找有关 Dates and Time 的更多信息

产品

标签

Community Treasure Hunt

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

Start Hunting!

Translated by