how to loop through variables names?

60 Ansichten (letzte 30 Tage)
Bas Dirriwachter
Bas Dirriwachter am 28 Okt. 2020
Beantwortet: Bas Dirriwachter am 29 Okt. 2020
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 Kommentare
Stephen23
Stephen23 am 28 Okt. 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
Bas Dirriwachter am 29 Okt. 2020
Bearbeitet: Bas Dirriwachter am 29 Okt. 2020
@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.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Bas Dirriwachter
Bas Dirriwachter am 29 Okt. 2020
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))

Weitere Antworten (0)

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Produkte


Version

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by