Create double workspace variable from Table

14 ビュー (過去 30 日間)
Olumide Ayeni
Olumide Ayeni 2021 年 2 月 24 日
回答済み: Mahesh Taparia 2021 年 2 月 27 日
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.
  2 件のコメント
Olumide Ayeni
Olumide Ayeni 2021 年 2 月 24 日
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.


回答 (1 件)

Mahesh Taparia
Mahesh Taparia 2021 年 2 月 27 日
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 =
Hope it will help!

Community Treasure Hunt

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

Start Hunting!

Translated by