Filter löschen
Filter löschen

Creating table: how to define variable names

160 Ansichten (letzte 30 Tage)
turquoise_squid
turquoise_squid am 5 Mai 2016
Bearbeitet: Deborah am 15 Jul. 2019
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!

Antworten (2)

Guillaume
Guillaume am 5 Mai 2016
Bearbeitet: Guillaume am 5 Mai 2016
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 am 15 Jul. 2019
Bearbeitet: Deborah am 15 Jul. 2019
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).

Kategorien

Mehr zu Tables finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by