Using If and Ifelse to create a variable

2 Ansichten (letzte 30 Tage)
Andrew
Andrew am 3 Jul. 2013
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 Kommentar
Evan
Evan am 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.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Evan
Evan am 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.

Weitere Antworten (0)

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by