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 日

1 投票

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 件のコメント

Daniel Shub
Daniel Shub 2011 年 10 月 25 日
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.
Daniel Shub
Daniel Shub 2011 年 12 月 12 日
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