How to build a table within an if loop without preallocation

조회 수: 5 (최근 30일)
Gavin Goddard
Gavin Goddard 2021년 4월 8일
댓글: Gavin Goddard 2021년 4월 8일
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

채택된 답변

David Fletcher
David Fletcher 2021년 4월 8일
편집: David Fletcher 2021년 4월 8일
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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Tables에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by