how to loop through variables names?
59 次查看(过去 30 天)
显示 更早的评论
I have an export from a FEA program (.dat file). I was able to convert them into a specifc array i need. Since they are imported they all have their own name so this is layer1, layer2 etc. I want to compare them so want to plot hem together. since these are over a 100 layers (and this has to be repeated over 16 times) i was hoping to create a loop in the variable name. i found a way to create a string with the corresponding variable names using the following code
(for trying i use just 5 layers)
n=5
L=string(zeros(n,1))
for i = 1:n
L(i)=string(sprintf('layer%d',i))
end
but since it is a string you cannot put this iside the plot command. therefore I tried to connect the name to the variable using the eval command
i know every matlab page recommendes to not use this command
z= eval(L(1))
When I recall z for L(1) it will give the array for L(1) and when I call for L(2) z will give me the correct array but i cannot loop this. Also a loop to combine them in one matrix failed.
When I googled a bit more i came accress cell arrays but I still cannot find a whay to succeed.
If I can loop through the variable names I can do both plotting all arrays and combine them into one matrix (which is also need).
3 个评论
Stephen23
2020-10-28
"Since they are imported they all have their own name so this is layer1, layer2 etc."
This importing is cause of your difficulties. This is the step that you should fix. But so far you have not given us any information on exactly how you imported the data: what function/s, with what code?
If you tell us a bit about the file importing, someone can help you to improve it (and avoid the bad code).
采纳的回答
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!