MATLAB: How to update x-axis limit of a TIMESERIES plot in MATLAB 7.8 (R2009a)

axischangelimitMATLABtimeseriesxx axis

I want to update x-axis limit of a TIMESERIES plot. However, using serial date numbers for minimum and maximum limits of the x-axis does not work. For example, I create a TIMESERIES plot:
b = timeseries(rand(1000,1), rand(1000,1)+[0:999]');
b.TimeInfo.StartDate = datestr( now );
f1 = figure;
plot(b);
Updating XLIM with the following:
xBegin = datenum(b.TimeInfo.StartDate,'dd-mmm-yyyy HH');
xEnd = datenum(b.TimeInfo.StartDate,'dd-mmm-yyyy HH')+ 0.0417;
xlim([xBegin xEnd])
incorrectly updates the x-axis limits.

Best Answer

As of MATLAB 7.8 (R2009a), the XDATA of a TIMESERIES plot is expressed in days relative to the StartDate of TIMESERIES (DATENUM(b.TimeInfo.StartDate) in the example) and not in absolute DATENUM values. This avoids rounding problem in plotting TIMESERIES with numerically small time values expressed relative to a large StartDate offset. To update the x-axis limits, use the time vector itself after converting to days, for example:
xBegin = 0; % starting x-limit relative to the StartDate
xEnd = 0.0417; % where 0.0417 is a fractional number of a day
xlim([xBegin xEnd])