i need to create variable name using for loop from a table

2 次查看(过去 30 天)
i'm trying to assign each column from my table to each separate variable with header as variable name using for loop
i couldn't find any function that directly does it
i've used fo loop to index tha data and i'm able to pront the collumns separately
when i try to assign contents of a table as variable name it is not allowing to go ahead
the following is my code
tableA has headers as variable names which are loaded in table T
file =("WLTCshortened_2.txt")
opts = detectImportOptions(file);
opts.DataLines = 3;
tableA=readtable(file,opts)
T = readtable('variable_name.xlsx', 'Range','A2:BV2', 'ReadVariableNames',false)
for i=(1:74)
rowname =string(table2cell(T(1,i)))
y=rowname.char
string(y)=(tableA.(string(y)))
end
  9 个评论
Vijayagopi Ramachnadran
temp = struct;
temp.(string(y))=(table.(string(y)));
save("tempData.mat","-struct","temp")
load tempData.mat
delete tempData.mat
clear temp
adding the above code has worked
Vijayagopi Ramachnadran
@Stephen i need dynamically named variable as i have around 80 variables to be analysed with simulink
that will help me with easy identification

请先登录,再进行评论。

采纳的回答

Vijayagopi Ramachnadran
temp = struct;
temp.(string(y))=(table.(string(y)));
save("tempData.mat","-struct","temp")
load tempData.mat
delete tempData.mat
clear temp
adding the above code has worked

更多回答(1 个)

Steven Lord
Steven Lord 2022-2-23
Can you define variables with numbered names like X1, X2, X3, ... ? Yes.
Should you do this? Generally we recommend against it. See that page for alternatives you should use instead.
  3 个评论
Steven Lord
Steven Lord 2022-2-23
Since you mentioned you want to do this in the context of working with Simulink, I'd consider using a model workspace or perhaps a data dictionary. See the documentation for more information on how to programmatically interact with a model workspace and this documentation section for more information about managing design data in general.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Workspace Variables and MAT Files 的更多信息

产品


版本

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by