How can I use timeseries objects with serial date numbers (datenum)?

2 views (last 30 days)
is it possible to use serial date numbers as x-values for a timeseries object?
In the following code I expect to see the specified format as x ticks.
y = rand(10,1);
timevec = [736140.6259375
736140.625940509
736140.62594537
736140.625947685
736140.625949988
736140.625952998
736140.625957859
736140.625960405
736140.62596272
736140.625965718];
ts = timeseries(y,timevec);
ts.Time = ts.Time - ts.Time(1); % relative time
ts.TimeInfo.Units = 'seconds';
ts.TimeInfo.Format = 'SS,FFF';
figure;
plot(ts);
best regards,
Fredrik

Answers (1)

Mukul Rao
Mukul Rao on 26 Jun 2015
Edited: Mukul Rao on 26 Jun 2015
From what I understand at the moment, this is possible provided a "StartDate" value is set and also if the available'Units' meaningfully define the increment between timevec values. Let me explain what I mean with an example:
y = rand(3,1);
timevec = datenum({'09/16/2007';'09/17/2007';'09/18/2007'})';
ts = timeseries(y,timevec);
ts.TimeInfo.StartDate = datestr(0);
ts.TimeInfo.Units = 'days';
ts.TimeInfo.Format = 'mmmm dd, yyyy';
figure('Units','normalized','Position',[0.2 0.2 0.5 0.5]);
plot(ts);
This will give the image,
Notice that the XTick labels are what is expected. Going back to the documentation for the 'Time' property of a timeseries object:
"When TimeInfo.StartDate is empty, the numerical Time values are measured relative to 0 in specified units. When TimeInfo.StartDate is defined, the time values are date strings measured relative to the StartDate in specified units."
In your original code, the time values were numerical because of a missing "StartDate". However, even with this addition, you would get a meaningless plot because none of the available 'Units' are meaningful for your timevec. The 'Units' for the case of datestr's, are literally used to increment the 'XTick' labels and the default number of ticks used appears to be the number of entries in timevec. So setting 'Units' to seconds for a timevec with 8 elements will give a plot that spans 8 seconds, whereas your timevec data is confined to an interval of 2 seconds.
I would avoid using the inbuilt datestr functionality in timeseries for such time vectors and manually label the plot as shown in the code below:
y = rand(10,1);
timevec = [736140.6259375
736140.625940509
736140.62594537
736140.625947685
736140.625949988
736140.625952998
736140.625957859
736140.625960405
736140.62596272
736140.625965718];
ts = timeseries(y,timevec);
ts.Time = ts.Time - ts.Time(1); % relative time
ts.TimeInfo.Units = 'seconds';
ts.TimeInfo.Format = 'SS.FFF';
figure('Units','normalized','Position',[0.2 0.2 0.5 0.5]);
plot(ts);
axesHandle = gca;
axesHandle.XTick = ts.Time;
axesHandle.XTickLabel = datestr(ts.Time,ts.TimeInfo.Format);
axesHandle.XTickMode = 'manual';

Categories

Find more on Variables in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!