New Workplace variable from Table

4 vues (au cours des 30 derniers jours)
Nathan Kelly
Nathan Kelly le 9 Mar 2023
Say I have a Table with Variable headers for each column such as below.
Tp = table(categorical({'M';'M';'F';'F';'F'}),[38;43;38;40;49],...
[71;69;64;67;64],[176;163;131;133;119],...
'VariableNames',{'Gender' 'Age' 'Height' 'Weight'})
If I look at the table in the variable window I can select the column by hand and create a new workplace variable/array from the data , named after the headers (VariableNames',{'Gender' 'Age' 'Height' 'Weight'})) with the data of that variable.
Is there a way to code a script to do this without manually picking the columns? Or a way to create new workplace variables from list?

Réponse acceptée

Steven Lord
Steven Lord le 9 Mar 2023
Is there a way to code a script to do this without manually picking the columns? Or a way to create new workplace variables from list?
Can you dynamically create variables with names automatically generated from table variable names? Yes.
Should you do this? The general consensus is no. That Answers post explains why this is generally discouraged and offers several alternative approaches.
Depending on what you want to do with those variables the varfun function may be of interest to you.

Plus de réponses (1)

Cameron
Cameron le 9 Mar 2023
Tp = table(categorical({'M';'M';'F';'F';'F'}),[38;43;38;40;49],...
[71;69;64;67;64],[176;163;131;133;119],...
'VariableNames',{'Gender' 'Age' 'Height' 'Weight'});
Age = Tp.Age;
Gender = Tp.Gender;
disp(Age)
38 43 38 40 49

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Produits


Version

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by