How to create a loop that runs through the rows of a table?
7 views (last 30 days)
Show older comments
Harry Jones
on 16 Mar 2021
Commented: Cris LaPierre
on 16 Mar 2021
I am trying to create a loop that runs through a table (2016 x 2) that takes the data from each column and inputs it into an equation and plots each result for each row. This is what I have attempted so far however, I am struggling to get this to work. Any help would be greatly appreciated.
rows = height(T);
for row = 1:rows
a1=T(row,1);
a2=T(row,2);
P=a1*a2;
end
hold on
plot(p,row)
0 Comments
Accepted Answer
Cris LaPierre
on 16 Mar 2021
Edited: Cris LaPierre
on 16 Mar 2021
You don't need a for loop for this.
P=T{:,1}.*T{:,2};
plot(P)
3 Comments
Cris LaPierre
on 16 Mar 2021
You need to capture the result from each loop. Right now, each loop overwrites the previous value with the current value. You want to keep them all.
T=readtable("T.xlsx")
E1=236;
E2=5;
G12=2.6;
v12=0.25;
v21=(v12*E2)/E1;
t=0.25;
N1=[1.5;3;0];
Q=[(E1/(1-v12*v21)) ((v12*E2)/(1-v12*v21)) 0; ((v21*E1)/(1-v12*v21)) (E2/(1-v12*v21)) 0; 0 0 G12];
rows = height( T);
Z1=0;
Z2=t;
Z3=2*t;
Z4=3*t;
Z5=4*t;
for row = 1:1:rows
a1=T{row,1};
a2=T{row,2};
%Matricies definition
T1=[cosd(a1)^2, sind(a1)^2, -2*cosd(a1)*sind(a1); sind(a1)^2, cosd(a1)^2, 2*cosd(a1)*sind(a1); cosd(a1)*sind(a1), -cosd(a1)*sind(a1), (cosd(a1)^2-sind(a1)^2)];
T2=[cosd(a2)^2, sind(a2)^2, -2*cosd(a2)*sind(a2); sind(a2)^2, cosd(a2)^2, 2*cosd(a2)*sind(a2); cosd(a2)*sind(a2), -cosd(a2)*sind(a2), (cosd(a2)^2-sind(a2)^2)];
Q1=T1*Q*transpose(T1);
Q2=T2*Q*transpose(T2);
A=(Q1*(Z2-Z1))+(Q2*(Z3-Z2))+(Q1*(Z4-Z3))+(Q2*(Z5-Z4));
B=(0.5*Q1*((Z2^2)-(Z1^2)))+(0.5*Q2*((Z3^2)-(Z2^2))+(0.5*Q1*(Z4^2)-(Z3^2)))+(0.5*Q2*((Z5^2)-(Z4^2)));
D=((1/3)*Q1*((Z2^3)-(Z1^3)))+((1/3)*Q2*((Z3^3)-(Z2^3))+((1/3)*Q1*(Z4^3)-(Z3^3)))+((1/3)*Q2*((Z5^3)-(Z4^3)));
e0=(A^-1)*N1;
e1=(transpose(T1))*e0;
e2=(transpose(T2))*e0;
s1=Q*e1;
s2=Q*e2;
sx1(row)=s1(1,1);
sy1(row)=s1(2,1);
end
plot(sx1,sy1,'.')
More Answers (1)
ANKUR KUMAR
on 16 Mar 2021
Edited: ANKUR KUMAR
on 16 Mar 2021
clc
clear
A=randi(20,25,5); %generating random data
T = array2table(A);
equation= @(x) x*5 + log(x) % creating an equation
names=T.Properties.VariableNames;
for kk=1:length(names)
values=T(:,names{kk});
plot(equation(values{:,:}))
hold on
end
0 Comments
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!