how to loop through variables names?

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 Comments

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!
"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).
@neil jerome, I think you are right about working as a struct but still have issues. I have attached a zip file containing one of the imported files. I noticed I have to import them as a table since a lot of data get lost when I try to import them as an array. after I have imported them I tried to substract the information I need (in this case it is the NODE number and the corresponding value for SX as can be seen in the attached file). The import of (just a small number of the final amount of layers) and creating a correct 224x2 table is done using the following code
structName(1).data = readtable('layer1.dat')
structName(2).data = readtable('layer2.dat')
structName(3).data = readtable('layer3.dat')
structName(4).data = readtable('layer4.dat')
structName(5).data = readtable('layer5.dat')
structName(6).data = readtable('layer6.dat')
structName(7).data = readtable('layer7.dat')
structName(8).data = readtable('layer8.dat')
structName(9).data = readtable('layer9.dat')
structName(10).data = readtable('layer10.dat')
structName(11).data = readtable('layer11.dat')
structName(12).data = readtable('layer12.dat')
structName(13).data = readtable('layer13.dat')
structName(14).data = readtable('layer14.dat')
structName(15).data = readtable('layer15.dat')
structName(16).data = readtable('layer16.dat')
%
n=16
for i=1:n
structName(i).data= removevars(structName(i).data,{'commando', 'output__Static', 'Structural','x_F3_','Var7','Var8','Var9','Var10','Var11'});
end
for i=1:n
structName(i).data=structName(i).data(~any(ismissing(structName(i).data),2),:)
end
For all layers, the first columns are the same, therefore I want to plot this on the X-axis and plot all the second columns on the y axis. I get the error message that for a table I need to use stackedplot, but i can only manage to plot column 2 over a range on the X-axis. I was not able to manage to convert the tables into array.
stackedplot(structName(1).data(:,2))
and i also require a table/matrix containing the Node numbers in column 1, the SX of layer 1 on column 2, SX layer2 on column 3 etc. So I need to combine all second columns of the table within the struct.

Sign in to comment.

 Accepted Answer

I managed to solve this issue. Thanks to the input from neil jerome. I have to import the layers per file, I was able to create a loop for the readtable command and was able to transfer the tables inside the struc to arrays inside the struct. see the code beneath
clc;
clear all ;
n=44;
for i=1:n
structName(i).data=readtable(sprintf('layer%d.dat',i));
end
for i=1:n
structName(i).data.Properties.VariableNames = {'NODE','SX','SY', 'SZ', 'SXY','SYZ','SXZ','Var8','Var9','Var10','Var11'};
end
for i=1:n
structName(i).data= removevars(structName(i).data,{'SY', 'SZ', 'SXY','SYZ','SXZ','Var8','Var9','Var10','Var11'});
end
for i=1:n
structName(i).data=structName(i).data(~any(ismissing(structName(i).data),2),:);
end
for i=1:n
structName(i).data_new1=zeros(224,2);
end
for i=1:n
structName(i).data_new=table2array(structName(i).data);
end
figure;
hold on
for ii = 1:n
plot(structName(ii).data_new(:,1),structName(ii).data_new(:,2));
end
E = (structName(1).data_new(:,1));
for i=1:n
E(:,i+1)=(structName(i).data_new(:,2));
end
%multiply every layer*layerthickness
P=zeros(224,2);
P(:,1)=structName(1).data_new(:,1);
for i=1:224
P(i,2)=sum(E(i,2:n)*0.23)/(0.23*n);
end
figure;
plot(P(:,1),P(:,2))

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products

Release

R2020b

Community Treasure Hunt

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

Start Hunting!