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

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 件のコメント

KSSV
KSSV 2022 年 2 月 23 日
You can straight away access them using:
either T.(1), T.(2) or T.varname1, T.varname2.
Why you want to assign them ino other variable?
Arif Hoq
Arif Hoq 2022 年 2 月 23 日
please attach your file
Vijayagopi Ramachnadran
Vijayagopi Ramachnadran 2022 年 2 月 23 日
編集済み: Vijayagopi Ramachnadran 2022 年 2 月 23 日
file is very big to attach
the file is is similar to this
table A
time speed brake
0 0 0
1 2 0
2 2 0
3 3 0
4 4 0
5 5 0
6 6 0
7 7 0
8 8 0
9 9 0
and table T
time speed brake
Vijayagopi Ramachnadran
Vijayagopi Ramachnadran 2022 年 2 月 23 日
@KSSV i need to separate each column to create a timeseries data
there are lot of variables
instead of doing manually like
speed=tableA.speed
i would like to use for loop to extract in all the variables
KSSV
KSSV 2022 年 2 月 23 日
for i = 1:width(T)
data = T.(i) ;
end
Vijayagopi Ramachnadran
Vijayagopi Ramachnadran 2022 年 2 月 23 日
編集済み: Vijayagopi Ramachnadran 2022 年 2 月 23 日
hai @KSSV this what i'm also doing
for i=(1:74)
rowname =string(table2cell(T(1,i)))
y=rowname.char
string(y)=(tableA.(string(y)))
end
i need to create sepate variables which has name of cell contents
here 74 is table width
it is not working
i need a way to use the string stored in y as a variable name
tried fprintf, string, springf
none of htese is working
Stephen23
Stephen23 2022 年 2 月 23 日
編集済み: Stephen23 2022 年 2 月 23 日
"i need a way to use the string stored in y as a variable name"
Why? What are you planning on doing with those dynamically-named variables?:
What possible benefit do you imagine that cannot be achieved using the syntaxes shown by KSSV?:
"i need to separate each column to create a timeseries data"
You can do that easily by looping over the table variables and storing the timeseries in a cell array, table, or structure. Did you try that before jumping to the conclusion that you need to dyanamically name variables?
Vijayagopi Ramachnadran
Vijayagopi Ramachnadran 2022 年 2 月 23 日
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
Vijayagopi Ramachnadran 2022 年 2 月 23 日
@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
Vijayagopi Ramachnadran 2022 年 2 月 28 日

0 投票

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 日

1 投票

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 件のコメント

Vijayagopi Ramachnadran
Vijayagopi Ramachnadran 2022 年 2 月 23 日
temp = struct;
temp.(string(y))=(table.(string(y)));
save("tempData.mat","-struct","temp")
load tempData.mat
delete tempData.mat
clear temp
this has helped in addition to above code
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.
Vijayagopi Ramachnadran
Vijayagopi Ramachnadran 2022 年 2 月 23 日
thanks steven

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeWorkspace 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