How to create a loop that runs through the rows of a table?

5 visualizzazioni (ultimi 30 giorni)
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)

Risposta accettata

Cris LaPierre
Cris LaPierre il 16 Mar 2021
Modificato: Cris LaPierre il 16 Mar 2021
You don't need a for loop for this.
P=T{:,1}.*T{:,2};
plot(P)
  3 Commenti
Harry Jones
Harry Jones il 16 Mar 2021
T is the table of variables I plan to use. a1 refers to the column a and a2 refers to column B. It should now be attached. Thanks once again for looking at this!
Cris LaPierre
Cris LaPierre il 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")
T = 2116×2 table
Var1 Var2 ____ ____ 30 30 30 31 30 32 30 33 30 34 30 35 30 36 30 37 30 38 30 39 30 40 30 41 30 42 30 43 30 44 30 45
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,'.')

Accedi per commentare.

Più risposte (1)

ANKUR KUMAR
ANKUR KUMAR il 16 Mar 2021
Modificato: ANKUR KUMAR il 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

Community Treasure Hunt

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

Start Hunting!

Translated by