how to loop through variables names?
Mostra commenti meno recenti
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 Commenti
neil jerome
il 28 Ott 2020
hi bas; not a lot of detail in your description: what is the data (list of numbers?), how are they being imported, etc? yes, you really should avoid eval and numerated variables - try importing into a field of a structure, so you have:
structName(1).data = importOfLayer1;
structName(2).data = importOfLayer2;
% etc
this will allow you to loop for plotting etc. by indexing within the (single) structure. hard to say more without any detail. if you cannot work with the import itself for whatever reason, and you absolutely have to deal with the numerated variables, recommend you do the eval immediately after import, once, and put everything into a structure (or multi-dimensional matrix, depending on what your data actually is) at the start, get it into a shape where you can loop, and never look back :)
good luck!
Stephen23
il 28 Ott 2020
"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).
Bas Dirriwachter
il 29 Ott 2020
Modificato: Bas Dirriwachter
il 29 Ott 2020
Risposta accettata
Più risposte (0)
Categorie
Scopri di più su Loops and Conditional Statements in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!