How to save variables in workspace to be used after running the code multiple times?

조회 수: 5 (최근 30일)
A = input('Pick an angle 15, 30, 45, or 60 angle = ');
if A ~= [15 30 45 60]
error('Error. angle must be 15, 30, 45, or 60 degrees ');
end
b = 4;
c = 4;
a = sidea(b,c,A);
B = asind((b*sind(A))/a);
C = 180 - B - A;
H = C;
E = 180 - C - H;
e = 4;
f = a;
g = 4;
h = 4;
F = asind((f*sind(E))/e);
D = 180 - E - F;
d = sided(e, f, D);
i = d;
G = asind((g*sind(H))/h);
I = 180 - G - H;
%% Variables/Calculations
% Method of Joints
P = 1;
% Joint I
Fig = P / sind(I);
Fei = Fig * cosd(I);
% Joint G
Fge = (Fig*sind(I))/(sind(F));
Fgb = (Fge*cosd(F)+Fig*cosd(I));
% Joint B
Fba = (Fgb)/(cosd(A)+(sind(A)*cotd(C)));
Fbe = (Fba*sind(A))/sind(C);
% Joint E
Fea = Fei - (Fge*cosd(F))+(Fbe*cosd(D));
%% Solving for P
if A == 15
Fea = -Fea;
if Fba > Fge
T = ((8*(trig(A, C, F, I))));
elseif Fba < Fge
T = 6 * sind(F);
end
end
if A == 30
Fea = -Fea;
if Fba > Fge
Z = ((8*(trig(A, C, F, I))));
elseif Fba < Fge
Z = 6 * sind(F);
end
end
if A == 45
Fea = -Fea;
if Fba > Fbe
P = (8*(trig(A, C, F, I)));
elseif Fbe > Fba
P = ((6*(trig(A, C, F, I)*(sind(C)/sind(A)))));
end
end
if A == 60
R = ((6*(trig(A, C, F, I))));
end
%% User Defined Functions
function [a] = sidea(b, c, A)
[a] = sqrt(b^2 + c^2 - (2*b*c*cosd(A)));
end
function [d] = sided(e, f, D)
[d] = sqrt(e^2 + f^2 - (2*e*f*cosd(D)));
end
function [comp] = trig(A, C, F, I)
[comp] = (cosd(A)+sind(A)*cotd(C))/(cotd(F)+cotd(I));
end
I am trying to run this code four times for four different angles, [15, 30, 45, 60]. I want it to save variables across each run so at the end, i can use them to plot. My problem is, no matter what i do, the variables seem to be overided. I tried to saved the variable as it's own file using save command but I could not run the code because i was trying to load variables that were not yet saved. I tried to initialize these variables and save them as 0 so that i could run the code but then the variables would be overun. If anyone has any idea it would be a great help.

답변 (1개)

Walter Roberson
Walter Roberson 2020년 4월 18일
편집: Walter Roberson 2020년 4월 18일
T = nan;
Z = nan;
P = nan;
R = nan;
for trynum = 1 : 4
A = input('Pick an angle 15, 30, 45, or 60 angle = ');
if A ~= [15 30 45 60]
error('Error. angle must be 15, 30, 45, or 60 degrees ');
end
b = 4;
c = 4;
a = sidea(b,c,A);
B = asind((b*sind(A))/a);
C = 180 - B - A;
H = C;
E = 180 - C - H;
e = 4;
f = a;
g = 4;
h = 4;
F = asind((f*sind(E))/e);
D = 180 - E - F;
d = sided(e, f, D);
i = d;
G = asind((g*sind(H))/h);
I = 180 - G - H;
%% Variables/Calculations
% Method of Joints
P = 1;
% Joint I
Fig = P / sind(I);
Fei = Fig * cosd(I);
% Joint G
Fge = (Fig*sind(I))/(sind(F));
Fgb = (Fge*cosd(F)+Fig*cosd(I));
% Joint B
Fba = (Fgb)/(cosd(A)+(sind(A)*cotd(C)));
Fbe = (Fba*sind(A))/sind(C);
% Joint E
Fea = Fei - (Fge*cosd(F))+(Fbe*cosd(D));
%% Solving for P
if A == 15
Fea = -Fea;
if Fba > Fge
T = ((8*(trig(A, C, F, I))));
elseif Fba < Fge
T = 6 * sind(F);
end
end
if A == 30
Fea = -Fea;
if Fba > Fge
Z = ((8*(trig(A, C, F, I))));
elseif Fba < Fge
Z = 6 * sind(F);
end
end
if A == 45
Fea = -Fea;
if Fba > Fbe
P = (8*(trig(A, C, F, I)));
elseif Fbe > Fba
P = ((6*(trig(A, C, F, I)*(sind(C)/sind(A)))));
end
end
if A == 60
R = ((6*(trig(A, C, F, I))));
end
end
if isnan(T)
error('You never selected 15')
end
if isnan(Z)
error('You never selected 30')
end
if isnan(P)
error('You never selected 45')
end
if isnan(R)
error('You never selected 60')
end
plot([15 30 45 60], [T, Z, P, R])
%% User Defined Functions
function [a] = sidea(b, c, A)
[a] = sqrt(b^2 + c^2 - (2*b*c*cosd(A)));
end
function [d] = sided(e, f, D)
[d] = sqrt(e^2 + f^2 - (2*e*f*cosd(D)));
end
function [comp] = trig(A, C, F, I)
[comp] = (cosd(A)+sind(A)*cotd(C))/(cotd(F)+cotd(I));
end
  댓글 수: 5
Walter Roberson
Walter Roberson 2020년 4월 18일
exactly which variables need to be recorded and at which points? For example do you need to record Fea before and after Fea = -Fea?
Stevie Rauch
Stevie Rauch 2020년 4월 18일
I think im okay now. I built a structure within the for loop to display the most important values. Thank you for the help though.

댓글을 달려면 로그인하십시오.

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by