How to build a table within an if loop without preallocation
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Gavin Goddard
le 8 Avr 2021
Commenté : Gavin Goddard
le 8 Avr 2021
I'm having trouble building this table in my code. Currently it just displays the last value that was calculated rather than creating a table with all the values calculated. I have seen a lot of answers with for loops but none with if. Is there something I am missing? I even get the warning on T that it changes size every iteration, however, it doesn't seem to actually do that.
% Safety Factors
SFy = 1.25; % Yield
SFu = 1.50; % Ultimate
SFb = 2.0; % Stability
% ____ Material Properties
E = 31182; % (ksi) Modulus of Elasticity
Sy = 91.5; % (ksi) Yield Strength
Su = 112; % (ksi) Ultimate Strength
D = 0.25; % (in) Initial Outside Diameter Value
d = 0.001; % (in) Initial Inside Diameter Value
L = 36; % (in) Length of Bar
P = 3; % (ksi) Applied Axial Force
j = 1; % Book-keeping
while D <= 2 && d <= 0.249
I = pi*(D^4-d^4)/64; % (in^4) Moment of Inertia
Pcrt = (pi^2*E*I)/(L^2); % Calculating Pcr
SFbt = Pcrt/P;
if SFbt >= SFb
A = pi*(D^2-d^2)/4; % (in^2) Area Value
s = P/A; % (ksi) Axial Stress = Force/Area
SFyt = Sy/s; % Calculating Safety Factor Yield
SFut = Su/s; % Calculating Saftey Factor Ultimate
T = table(); % Storage Table
tempTable = table();
if SFyt >= SFy && SFut >= SFu % Checking for Validity
tempTable.D = D;
tempTable.d = d;
tempTable.A = A;
tempTable.s = s;
tempTable.Pcrt = Pcrt;
tempTable.SFut = SFut;
tempTable.SFyt = SFyt;
tempTable.SFbt = SFbt;
T = [T; tempTable];
end
end
if D >= 1.989 % Running Through Possibilities
d = d + 0.001;
D = 0.25;
end
D = D + 0.01;
j = j+1;
end
0 commentaires
Réponse acceptée
David Fletcher
le 8 Avr 2021
Modifié(e) : David Fletcher
le 8 Avr 2021
Doubtless, you will kick yourself - but put your main table declaration outside the loop, or you'll be overwriting the old main table and everything you put in it with a new table on every iteration.
% Safety Factors
SFy = 1.25; % Yield
SFu = 1.50; % Ultimate
SFb = 2.0; % Stability
% ____ Material Properties
E = 31182; % (ksi) Modulus of Elasticity
Sy = 91.5; % (ksi) Yield Strength
Su = 112; % (ksi) Ultimate Strength
D = 0.25; % (in) Initial Outside Diameter Value
d = 0.001; % (in) Initial Inside Diameter Value
L = 36; % (in) Length of Bar
P = 3; % (ksi) Applied Axial Force
j = 1; % Book-keeping
T = table(); % Storage Table <----------------------- PUT TABLE DECLARATION BEFORE TEH LOOP
while D <= 2 && d <= 0.249
I = pi*(D^4-d^4)/64; % (in^4) Moment of Inertia
Pcrt = (pi^2*E*I)/(L^2); % Calculating Pcr
SFbt = Pcrt/P;
if SFbt >= SFb
A = pi*(D^2-d^2)/4; % (in^2) Area Value
s = P/A; % (ksi) Axial Stress = Force/Area
SFyt = Sy/s; % Calculating Safety Factor Yield
SFut = Su/s; % Calculating Saftey Factor Ultimate
% T = table(); <---------------------- THIS IS THE PROBLEM
tempTable = table();
if SFyt >= SFy && SFut >= SFu % Checking for Validity
tempTable.D = D;
tempTable.d = d;
tempTable.A = A;
tempTable.s = s;
tempTable.Pcrt = Pcrt;
tempTable.SFut = SFut;
tempTable.SFyt = SFyt;
tempTable.SFbt = SFbt;
T = [T; tempTable];
end
end
if D >= 1.989 % Running Through Possibilities
d = d + 0.001;
D = 0.25;
end
D = D + 0.01;
j = j+1;
end
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur MISRA C++:2008 Rules dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!