Using the new datetime structure within datasets

9 views (last 30 days)
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?

Accepted Answer

Peter Perkins
Peter Perkins on 24 Feb 2015
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 Comment
Jesse Blocher
Jesse Blocher on 24 Feb 2015
Great! I didn't even know about tables. I had been using datatables for a while and must have missed that new structure.

Sign in to comment.

More Answers (0)

Categories

Find more on Dates and Time in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!