Using the new datetime structure within datasets

11 ビュー (過去 30 日間)
Jesse Blocher
Jesse Blocher 2015 年 2 月 24 日
コメント済み: Jesse Blocher 2015 年 2 月 24 日
I have a dataset with date variables as datenums. I'd like to convert them to the new datetime datatype. Here is what I did:
ds.newdate = datetime(ds.olddate,'ConvertFrom','datenum');
This is what I get:
ds
ans =
olddate newdate
734138 [1x1 datetime]
This isn't useful. What I want is this:
ds
ans =
olddate newdate
734138 31-Dec-2009
There is some sort of sub-indexing going on where I have to go another level deeper to access my date variable. How do I access it directly in the dataset?

採用された回答

Peter Perkins
Peter Perkins 2015 年 2 月 24 日
Jesse, have you tried using a table rather than a dataset? A table is, roughly speaking, a replacement for dataset that is available in core MATLAB (rather than just the Statistics Toolbox) since R2013b. Since you are using datetime, you must have R2014b.
The [1x1 datetime] that you are seeing is really just a display artifact, and ds.newdate is a datetime. But table provides the display you are looking for:
>> t = table((734138:734140)','VariableNames',{'OldDate'})
t =
OldDate
__________
7.3414e+05
7.3414e+05
7.3414e+05
>> t.NewDate = datetime(t.OldDate,'ConvertFrom','datenum')
t =
OldDate NewDate
__________ ____________________
7.3414e+05 31-Dec-2009 00:00:00
7.3414e+05 01-Jan-2010 00:00:00
7.3414e+05 02-Jan-2010 00:00:00
Hope this helps.
  1 件のコメント
Jesse Blocher
Jesse Blocher 2015 年 2 月 24 日
Great! I didn't even know about tables. I had been using datatables for a while and must have missed that new structure.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeDates and Time についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by