MATLAB: Can I reference dates, instead of element index numbers, when referencing a time series

datesEconometrics ToolboxMATLABStatistics and Machine Learning Toolboxsub-samplestime series

I would like to reference certain sub-samples from my time series, which are quarterly economic data. I have many series with differing start and finish dates, but I want to split them all into sub-samples, at the same date, before performing some operations on them.
For example:
Series X1 extends from 31.3.1975 to 31.12.2017
Series X2 extends from 30.6.1983 to 31.12.2009
I want to split them both at 31.12.2000 into:
X1old from 31.3.1975 to 31.12.2000 and X1new from 31.3.2001 to 31.12.2017
X2old from 30.6.1983 to 31.12.2000 and X2new from 31.3.2001 to 31.12.2009
Can this be done by referencing dates, rather than element index numbers in the time series vectors themselves?

Best Answer

Use the dates to extract the desired indices. Look at the following example,
% Create some dummy data
t = datetime([2018 1 1 1 1 1]):days(30):datetime([2018 6 1 1 1 1]);
X = table(t.', rand(numel(t),1))
% find indices of the elements which are before 31.3.2018
indx = X{:,1}<=datetime([2018 3 31 23 0 0]);
% the corresponding elements
X(indx,:)
% other elements
X(~indx,:)