Azzera filtri
Azzera filtri

Creating table: how to define variable names

160 visualizzazioni (ultimi 30 giorni)
turquoise_squid
turquoise_squid il 5 Mag 2016
Modificato: Deborah il 15 Lug 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!

Risposte (2)

Guillaume
Guillaume il 5 Mag 2016
Modificato: Guillaume il 5 Mag 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 il 15 Lug 2019
Modificato: Deborah il 15 Lug 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).

Categorie

Scopri di più su Tables in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by