Creating table: how to define variable names

135 次查看(过去 30 天)
How can I define Variable names for each column after creating a table from a single variable with several columns?
For instance, when creating a table from one matrix x, MATLAB defines one name for the whole matrix irrespective of its column size. The only way to circumvene this problem is to subdefine each column of the matrix as a seperate variable. In my case I have go a matrix with 150 columns and it is very time consuming to do it this way:
table(x(:,1),x(:,2),..., x(:,150), 'VariableNames',{'Gender' 'Age' ... 'Sex'})
thank you for your assistance!

回答(2 个)

Guillaume
Guillaume 2016-5-5
编辑:Guillaume 2016-5-5
The way to circumvent this problem is to use the proper function to create a table from a matrix which is array2table (see also cell2table and struct2table):
t = array2table(x)

Deborah
Deborah 2019-7-15
编辑:Deborah 2019-7-15
I have however noticed that you may have to use the exact names of the column vectors you are passing to the 'table' function for that to work. For example:
Gender = x(:,1); Age = x(:,2);
T = table(Gender,Age,'VariableNames',{'Gender','Age'})
Note that you can only use array2table when the array entries are homogenous, but the above works for mixed entries (for example if Gender is a column vector of strings while Age is a numeric column vector).

类别

Help CenterFile Exchange 中查找有关 Tables 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by