MATLAB: Merge tables with different dimensions

table

Hi,
I'm trying to use vertcat to add together a sequence of tables. The problem is, some are 24×22, some are 24×19, etc.
Matlab returns the error
Error using table/vertcat (line 56)
All tables in the bracketed expression must have the same number
of variables.
Is there a way to work around this? Like, adding empty columns?
Thanks so much.

Best Answer

tables like matrices must have the same number of columns (variables with regards to tables) to concatenate them vertically. In addition, for tables, the variable names must be identical.
You can of course add variables to either table until all the variable names match:
t1 = array2table(magic(5), 'VariableNames', {'a', 'k', 'o', 'p', 'w'});
t2 = array2table(magic(6), 'VariableNames', {'a', 'p', 'o', 'v', 's', 'k'});
t1colmissing = setdiff(t2.Properties.VariableNames, t1.Properties.VariableNames);
t2colmissing = setdiff(t1.Properties.VariableNames, t2.Properties.VariableNames);
t1 = [t1 array2table(nan(height(t1), numel(t1colmissing)), 'VariableNames', t1colmissing)];
t2 = [t2 array2table(nan(height(t2), numel(t2colmissing)), 'VariableNames', t2colmissing)];
t = [t1; t2]