matlabfilterextractpanel-datatimetable

Matlab: Filter a timetable to balance a panel data


I have a timetable and I want to keep those days that coincide between each value from my variable "Id". In the example I put here, I start with my timetable T where there are three different "Id". My goal would be to end up with a timetable T_1 where only the observations where the dates coincide between each "Id" are kept.

T= timetable(datetime({'13/04/2018';'25/04/2018';'28/04/2018';'13/04/2018';'25/04/2018';'13/04/2018'}), [1;1;1;2;2;3],[30;29;45;21;24;8] );
T.Properties.VariableNames = {'Id' 'Price'}

%I want to get:

T_1=timetable(datetime({'13/04/2018';'13/04/2018';'13/04/2018'}), [1;2;3],[30;21;8] );

I woudl be really thankful if you can help me!


Solution

  • You can use

    T_1 = T('13/04/2018',:);
    

    I would advice you to read the docs about timerange and timetables in general.

    If you want to split the timetable in multiple smaller timetables, you can select them by date, and store them in a cell array:

    T_unique = unique(T.Time); % get all unique dates
    T_all = cell(size(T_unique)); % init an empty cell array
    for k = 1:numel(T_unique)
        T_all{k} = T(T_unique(k),:); % and put the timetable for one date in the cell array
    end