"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?
Show older comments
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 Comments
""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)
all(x<0)
all(x==0)
all(y>0)
all(y<0)
all(y==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.
Accepted Answer
More Answers (0)
Categories
Find more on Fourier Analysis and Filtering in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!