Effacer les filtres
Effacer les filtres

Setting array names from a table column name

35 vues (au cours des 30 derniers jours)
MARCO CESARI
MARCO CESARI le 4 Déc 2021
Modifié(e) : Adam Danz le 4 Déc 2021
Hi to everyone, i have a table in wich every column has a specific name, i want create one array for each column of table and i want to setting the name of this array equal to the name of the column table related to this one. I try to extract the column name of table using:
VarName=PL1.Properties.VariableNames
where 'PL1' is my table. After doing this i don't know how using the caracters in VarName to setting the name of an array witouth using a struct.
Can somone help me?
Thanks in advance

Réponses (1)

Adam Danz
Adam Danz le 4 Déc 2021
Modifié(e) : Adam Danz le 4 Déc 2021
Keep the data in a table
The table already contains the data in a tidy format. Deconstructing the table into separate variables makes the data management more complex, more prone to error, and more difficult to work with. Instead, use table indexing and keep all of you data in the tidy table.
For example, if table T has a variable named Population, instead of separating that variable from the table to use it in a function, just index the table, func(T.Population, __) or if you prefer using column indexing and want to reference column 2, func(T{:,2},__).
Copying variables from a table
Sometimes you may need to remove variables from a table rather than indexing or adding a new column to the table. In those cases you should assign your own variable name rather than relying on the variable names used in the table. Programmatically naming a variable is called dynamic variable naming and there are really good reasons to avoid this approach as Walter Roberson commented. I'll give just one example. Let's say the first variable of the table is named "score" and your dynamically named variable is therefore also named "score". Later in the code this variable is referenced. But 2 months from now somehow the table variables changed and now it's "Score" or "Scores". Now your code will break since your code uses the undefined name score. What is worse is that it may be very difficult to see where the undefined variable is supposed to come from since it's never explicitly defined in the code. All you see is that there should be a variable named "Scores" but it's nowhere to be found. It's bad programming. Instead, assign the data to your own variable name that you know will not change because you're defining it upon assignment.

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Produits


Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by