필터 지우기
필터 지우기

"theta" is defined inside of the statement, but when I run my code it gives me an error saying that " 'theta' is not a recognized function or variable'. Any solutions?

조회 수: 3 (최근 30일)
x = [2 2 0 -3 -2 -1 0 0 2];
y = [0 1 3 1 0 -2 0 -2 2];
r = (sqrt((x.^2)+(y.^2)));
if x > 0
theta = atan(y./x);
elseif x < 0
theta = atan(y./x);
if y > 0
theta = atan(y./x) + (pi);
elseif y < 0
theta = atan(y./x) - (pi);
elseif y == 0
theta = pi;
end
elseif x == 0
if y > 0
theta = (pi./2);
elseif y < 0
theta = (-pi./2);
elseif y == 0
theta = 0;
end
end
theta = rad2deg(theta); % Changing theta from radians to degrees
w = [x' y' r' theta']; % A matrix containing all of the values
  댓글 수: 2
Brendan
Brendan 2023년 1월 28일
편집: Brendan 2023년 1월 28일
When I go and define 'theta' outside of the if statment, Matlab just bypasses the if statement entirely and only uses the defined value.
Stephen23
Stephen23 2023년 1월 28일
""theta" is defined inside of the statement"
Nope. As the IF documentation states, "An expression is true when its result is nonempty and contains only nonzero elements". Lets check which of your logical expressions are considered true:
x = [2,2,0,-3,-2,-1,0,0,2];
y = [0,1,3,1,0,-2,0,-2,2];
all(x>0)
ans = logical
0
all(x<0)
ans = logical
0
all(x==0)
ans = logical
0
all(y>0)
ans = logical
0
all(y<0)
ans = logical
0
all(y==0)
ans = logical
0
So none of them are true, none of your IF/ELSEIF will run, and THETA is never defined inside them.
In any case, you should be using indexing for this task, not IF/ELSEIF. Or ATAN2.

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

채택된 답변

cdawg
cdawg 2023년 1월 28일
x is a vector so when you say "if x > 0" it is returning a logic vector of the locations where that is the case. You need to loop through:
X = [2 2 0 -3 -2 -1 0 0 2];
Y = [0 1 3 1 0 -2 0 -2 2];
R = (sqrt((X.^2)+(Y.^2)));
for ii = 1:length(X)
x = X(ii);
y = Y(ii);
r = R(ii);
if x > 0
theta = atan(y./x);
elseif x < 0
theta = atan(y./x);
if y > 0
theta = atan(y./x) + (pi);
elseif y < 0
theta = atan(y./x) - (pi);
elseif y == 0
theta = pi;
end
elseif x == 0
if y > 0
theta = (pi./2);
elseif y < 0
theta = (-pi./2);
elseif y == 0
theta = 0;
end
end
THETA(ii) = theta;
end
THETA = rad2deg(THETA); % Changing theta from radians to degrees
w = [X' Y' R' THETA']; % A matrix containing all of the values
  댓글 수: 3
Stephen23
Stephen23 2023년 1월 28일
편집: Stephen23 2023년 1월 28일
"You need to loop through"
No, you don't need a loop. This is easy to solve using exactly one command and no loop. Lets try:
X = [2,2,0,-3,-2,-1,0,0,2];
Y = [0,1,3,1,0,-2,0,-2,2];
R = (sqrt((X.^2)+(Y.^2)));
for ii = 1:length(X)
x = X(ii);
y = Y(ii);
r = R(ii);
if x > 0
theta = atan(y./x);
elseif x < 0
theta = atan(y./x);
if y > 0
theta = atan(y./x) + (pi);
elseif y < 0
theta = atan(y./x) - (pi);
elseif y == 0
theta = pi;
end
elseif x == 0
if y > 0
theta = (pi./2);
elseif y < 0
theta = (-pi./2);
elseif y == 0
theta = 0;
end
end
THETA(ii) = theta;
end
T1 = rad2deg(THETA) % Changing theta from radians to degrees
T1 = 1×9
0 26.5651 90.0000 161.5651 180.0000 -116.5651 0 -90.0000 45.0000
T2 = atan2d(Y,X) % the simple MATLAB approach
T2 = 1×9
0 26.5651 90.0000 161.5651 180.0000 -116.5651 0 -90.0000 45.0000
There you are: one simple function that gives the same output (to within floating point precision).
No loops or complex IF/ELSEIF required.
cdawg
cdawg 2023년 1월 28일
Thanks Stephen. I guess I shouldn’t have said you *need* to loop through. In fact that’s something I’m trying to work on as it’s usually my “goto” method, but is often unnecessary :-)

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by