Zed Kay
Zed Kay

Reputation: 133

Changing column names for an existing table

I have the following table in MATLAB:

A= 

    86   84
    45   65
     2   42
    44   29

MATLAB automatically returns for this table the column names A1 and A2, with A being the set name of the table, for the two columns.

How can I manually change the names of each column?

Upvotes: 1

Views: 1776

Answers (1)

Tommaso Belluzzo
Tommaso Belluzzo

Reputation: 23675

That can be easily accomplished using the VariableNames parameter of the array2table function (the same is valid for the cell2table function too), as follows:

A = [
  86   84
  45   65
   2   42
  44   29
];

T = array2table(A,'VariableNames',{'X' 'Y'})

The output table is:

T =

    X     Y 
    __    __

    86    84
    45    65
     2    42
    44    29

For an already existing table, you can use the same property on the instance itself in order to change its column names:

A = [
  86   84
  45   65
   2   42
  44   29
];

T = array2table(A,'VariableNames',{'X' 'Y'})
T.Properties.VariableNames = {'U' 'V'};
T

Take a look at the outputs:

T =

    X     Y 
    __    __

    86    84
    45    65
     2    42
    44    29



T =

    U     V 
    __    __

    86    84
    45    65
     2    42
    44    29

Upvotes: 6

Related Questions