MATLAB: Using for loop to match quarterly series with daily time series

for looplooploopsMATLABnantime series

I have two time series span over the period between 2000-01-01 and 2017-04-01, one is daily data of stock returns with 4500 observations, and the other one is quarterly with 69 observations. I want to make the quarterly data as lenghty as the daily series by repeating the quarterly observation all over the daily dates of the corresponding quarter, in each quarter there is 66 trading days, so each quarterly observation should be repeated 66 times. I tried to use for loop to do that
y = xlsread('Stock.xlsx','sheet1','B2:B4501') ./ 100; %importing the daily series
nobs = size(y,1);
xQuarter = xlsread('Stock.xlsx','sheet2','B2:B70');%importing the quarterly series
[~,yDate] = xlsread('Stock.xlsx','sheet1','A2:A4501');%importing the dates of the daily series
[~,yDateQuarter] = datevec(yDate);%this vector is created for repeating the quartely observations
xDay = NaN(nobs,1);%creating vector of NaNs matching the lenght of the daily series
count = 1;
%Running the for loop
for t = 1:nobs
if t > 1 && yDateQuarter(t) ~= yDateQuarter(t-1)
count = count + 1;
if count > length(xQuarter)
break
end
end
xDay(t) = xQuarter(count);
end
The 'xDay' is the vector of the repeated quarterly observations that is supposed to match the daily series, after running the code I'm still getting NaNs in this vector
How can I fix this problem?

Best Answer

  • If you can convert your data to timetables, I'd use the retime function. To do that, you might want to consider updating your import function to use readtable or readtimetable instead of xlsread.