Andi
Andi

Reputation: 4855

MATLAB: Integrate one timetable into another

I would like to expand an existing timetable. The original timetable consists of trading days only, while I would like to expand it to reflect all weekdays (no matter if working day or not).

Consider the original timetable tt. I would like to expand it using the RowTimesvector datesWeekdays. The expanded rows should contain NaNs.

When playing around, I was wondering if it makes sense to firstly create a new timetable comprising all dates and setting the data to NaN and subsequently "integrate" the original timetable tt into the new timetable ttWeekdays.

Here's a toy example:

data = rand(3,2);
dates = datetime({'2018-04-30', '2018-05-02', '2018-05-03'}, 'InputFormat','yyyy-MM-dd');
tt = array2timetable(data, 'RowTimes', dates, 'VariableNames', {'high', 'low'})

datesWeekdays = datetime({'2018-04-30', '2018-05-01', '2018-05-02', '2018-05-03', '2018-05-04'}, 'InputFormat','yyyy-MM-dd');
dataWeekdays = nan(5,2);
ttWeekdays = array2timetable(dataWeekdays, 'RowTimes', datesWeekdays, 'VariableNames', {'high', 'low'})

Upvotes: 1

Views: 223

Answers (1)

Edric
Edric

Reputation: 25140

You can use retime for this. Something like this:

% Set up a date vector
newDates = datetime('2018-04-30'):datetime('2018-05-10');
newDates(isweekend(newDates)) = [];

% Use RETIME in default mode to expand input table
retime(tt, newDates)

which gets

  9×2 timetable
       Time         high        low  
    ___________    _______    _______
    30-Apr-2018    0.81472    0.91338
    01-May-2018        NaN        NaN
    02-May-2018    0.90579    0.63236
    03-May-2018    0.12699    0.09754
    04-May-2018        NaN        NaN
    07-May-2018        NaN        NaN
    08-May-2018        NaN        NaN
    09-May-2018        NaN        NaN
    10-May-2018        NaN        NaN

Note that retime supports a variety of methods for filling in the missing data - here we're using the default which is 'fillwithmissing'.

Upvotes: 1

Related Questions