Search code examples
matlabmatlab-table

How can I programmatically group table column variables MATLAB?


If I create a table with:

t = table(magic(3));

I get a table with a Singular Variable Name

Table with Singular Variable Name

However if I:

a = magic(3);
T = array2table(a);

Then I get a table with Three Variable Names:

Table with Three Variable Names

If I try to group the columns by sending it only one variable name for the table:

T.Properties.VariableNames = {'OneName'};
The VariableNames property must contain one name for each variable in the table.

In the second situation, there is an option to combine the columns into one column manually by highlighting the columns and right clicking on the mouse.

Manually Combined 3 Variables into 1 Variable

How can I programmatically group the three variables to become one Variable as in the first example if I already created the matrix a ?

EDIT:

*as in the first example if I already created the table a ?

I am using R2017b

Based on the comment below, I am asking how to do mergevars prior to R2018a.

In the above example, I would be able to group them into one variable with:

t = table(a);

In other words, I hoped to create multiple multicolumn variables. In other-other words, to do mergevars prior to R2018a.


Solution

  • Once the table T has been created with a variable name for each column, the column values could be extracted, and then assigned back to T:

    b = T{:, 1:2};
    c = T{:, 3};
    T = table(b, c);
    

    enter image description here