Using the new datetime structure within datasets

10 Ansichten (letzte 30 Tage)
Jesse Blocher
Jesse Blocher am 24 Feb. 2015
Kommentiert: Jesse Blocher am 24 Feb. 2015
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?

Akzeptierte Antwort

Peter Perkins
Peter Perkins am 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 Kommentar
Jesse Blocher
Jesse Blocher am 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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Dates and Time finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by