MATLAB: How to concatenate tables vertically

MATLAB

How can I concatenate two or more tables vertically if they have the same headers?

Best Answer

To concatenate two or more tables vertically, you can use "vertcat" either as a function or as an operator - just as you would do to vertically concatenate two or more arrays. The headers do not even need be in the same order. Here is an example:
myTable1 = array2table(eye(3));
myTable1.Properties.VariableNames = ["One","Two","Three"];
myTable2 = array2table(2*eye(3));
myTable2.Properties.VariableNames = ["Two","Three","One"];
myTable3 = array2table(3*eye(3));
myTable3.Properties.VariableNames = ["Three","One","Two"];
myTable = [ myTable1; myTable2; myTable3 ]
The documentation page for "vertcat" explains that this function accepts table inputs: