MATLAB: Remove variables not shared by 4 tables

intersectshared_variablestable

Hello. How can I remove the variables not shared by 4 tables? Tables have the same number of rows but different number of variables (columns)
The objective is to get the 4 tables but only w variables that are present in each of the 4 individual tables.
I can only do it by pairs with "intersect"
[pair_a, pair_b, pair_c]=intersect(tab1.Properties.VariableNames, tab2.Properties.VariableNames);
tab1=tab1(:,pair_b);
tab2=tab2(:,pair_c);
But then I have to pit tab3 against tab1 and tab2, and then tab4. For 4 tables not too much hassle but as the number of tables increases it gets messier.

Best Answer

Create a cell array of the tables. For the moment I will call this tab_cell . Then
shared_vars = tab_cell{1}.Properties.VariableNames;
for K = 2 : numel(tab_cell)
shared_vars = interset(shared_vars, tab_cell{K}.Properties.VariableNames);
end
You do not need to compare each table to each other table: A intersect B intersect C = (A intersect B) intersect C = A intersect (B intersect C) = (A intersect C) intersect B -- intersection is transitive and commutative.