Effacer les filtres
Effacer les filtres

Using If and Ifelse to create a variable

1 vue (au cours des 30 derniers jours)
Andrew
Andrew le 3 Juil 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 commentaire
Evan
Evan le 3 Juil 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.

Connectez-vous pour commenter.

Réponse acceptée

Evan
Evan le 3 Juil 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.

Plus de réponses (0)

Catégories

En savoir plus sur Get Started with MATLAB dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by