Effacer les filtres
Effacer les filtres

If statements comparing two character strings with multiple inputs

7 vues (au cours des 30 derniers jours)
Mikela Petersen
Mikela Petersen le 16 Fév 2021
Commenté : Mikela Petersen le 16 Fév 2021
I have to use two strings that include different characters such as str1= ["rho" , "mu" , "nu"] and str2= ["SI", "BG"] and go from there. I need to use IF statements to get the correct values if i called the function from the command line: h20_props(50,"nu","SI) but when I do this, nothing happens. I don't even get an error. it's just nothing. Here's what I have:
function h20_props(T, str1, str2)
str1 = ["rho","mu","nu","pv"];
str2 = ["SI", "BG"];
if strcmpi(str1,"mu")
if strcmpi(str2, "SI")
dv = h20_mu_SI(T);
fprintf('dv_si = %g\n',dv);
else
dv = h20_mu_BG(T);
fprintf('dv_bg = %g\n',dv);
end
elseif strcmpi(str1,"nu")
if strcmpi(str2,"SI")
kv = h20_nu_SI(T);
fprintf('kv_si=%g\n',kv);
else
kv = h20_nu_BG(T);
fprintf('kv_bg=%g\n',kv);
end
elseif strcmpi(str1, "pv")
if strcmpi(str2, "SI")
vp = h20_pv_SI(T);
fprintf('vp_si=%g\n',vp);
else
vp = h20_pv_BG(T);
fprintf('vp_bg=%g\n',pv);
end
end
end
  1 commentaire
per isakson
per isakson le 16 Fév 2021
You overwrite the input arguments with the statements
str1 = ["rho","mu","nu","pv"];
str2 = ["SI", "BG"];
Comment out these two statements.

Connectez-vous pour commenter.

Réponse acceptée

Fangjun Jiang
Fangjun Jiang le 16 Fév 2021
variable 'str1' and 'str2' are supposed to take values from function input arguments when you call h20_props(50,"nu","SI"). But inside your fuction, the values were over-written in the first two lines of code. What are you trying to do?
I believe it should work if you comment out those two lines.
  1 commentaire
Mikela Petersen
Mikela Petersen le 16 Fév 2021
this was exactly what i was doing wrong. I commented the two string lines out and now everything works! Thank you!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Characters and Strings 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