Create double workspace variable from Table

11 vues (au cours des 30 derniers jours)
Olumide Ayeni
Olumide Ayeni le 24 Fév 2021
Hi.
I have a table in my workspace. I would like to create new workspace variable of type - double, with the heading of each table column as the variable name.
For example:
Assuming the table is already loaded into my workspace, I would like a script that automatically creates the variables: A, B, C with the data under the respective columns as values; and the type of the created workspace variable should be double.
Note: I want the script such that it reads the variable names A, B, C dynamically, cos these names could vary in the project.
Thanks
  2 commentaires
Stephen23
Stephen23 le 24 Fév 2021
Modifié(e) : Stephen23 le 24 Fév 2021
What is the reason that you cannot access the data directly from within the table?
"I want the script such that it reads the variable names A, B, C dynamically, cos these names could vary in the project."
If the names could vary, then magically defining them as variable names will make it difficult to keep track of them (as well as making your code slow, complex, and inefficient). In contrast, keeping them in the table makes it trivial to know what names have been supplied (and very efficient to process them).
What is the reason that you want to introduce latent bugs into your code for no obvious benefit?
Olumide Ayeni
Olumide Ayeni le 24 Fév 2021
Thanks for your response.
I have a simulink model that gets inputs from the workspace with the 'fromWorkspace' block, which won't take the data from the table.

Connectez-vous pour commenter.

Réponses (1)

Mahesh Taparia
Mahesh Taparia le 27 Fév 2021
Hi
You can always access the variable name using the table object. Consider an example below
Height = [71;69;64;67;64];
Weight = [176;163;131;133;119];
T = table(Height,Weight);
tableVar = T.Properties.VariableNames;
The tableVar stores the variable name in a cell array, you can access the value by calling the respective variable from the table object. For example:
>> T.(tableVar{1,1})
ans =
71
69
64
67
64
Hope it will help!

Catégories

En savoir plus sur Tables dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by