Using If and Ifelse to create a variable

6 views (last 30 days)
I am trying to create a single variable using if and ifelse. I can do this in excel and it comes up fine, so I know it must be something I am doing wrong. This is referencing two variables to create a new variable Theta_dist. This variable has four different conditions shown below. Does anyone know what I am doing wrong?
if Rad_cold>0 & Rad_hot>0 Theta_dist= atan(Hot_Side./Cold_Side); elseif Rad_cold<=0 & Rad_hot>0 Theta_dist = 1/2*pi + atan(-1*Cold_Side./Hot_Side); elseif Rad_cold<=0 & Rad_hot<=0 Theta_dist = pi + atan(Hot_Side./Cold_Side); else Rad_cold>0 & Rad_hot<=0 Theta_dist = 3/2*pi + atan (-Cold_Side./Hot_Side); end
  1 Comment
Evan
Evan on 3 Jul 2013
if Rad_cold>0 & Rad_hot>0
Theta_dist= atan(Hot_Side./Cold_Side);
elseif Rad_cold<=0 & Rad_hot>0
Theta_dist = 1/2*pi + atan(-1*Cold_Side./Hot_Side);
elseif Rad_cold<=0 & Rad_hot<=0
Theta_dist = pi + atan(Hot_Side./Cold_Side);
else Rad_cold>0 & Rad_hot<=0
Theta_dist = 3/2*pi + atan (-Cold_Side./Hot_Side);
end
I formatted your code to make it a little more readable.

Sign in to comment.

Accepted Answer

Evan
Evan on 3 Jul 2013
The last else statement is incorrect. If you use else instead of elseif you are basically saying "all other conditions not yet covered." So, if you use else, you can't follow it with any conditions. It's just the catch-all for any "other" situations.
If looks like you're trying to cover all possible combinations of the signs of rad_cold and rad_hot, so removing the condition after else (the part that reads "Rad_cold>0 & Rad_hot<=0" is required. Alternatively, you could change that last else to an elseif.

More Answers (0)

Categories

Find more on Get Started with MATLAB in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!