How do I Take in input string and insert that string into an if statment

5 次查看(过去 30 天)
I want to take a user input of two options that are strings, either SI or imperial units, and insert this into an if statement in order to execute one script if SI, and go to another else statement if imperial units.
Here's my code condensed:
H = input('Enter the word SI or imperial for Temp., pressure and density units','s')
if input == 'SI'
H = input('Enter Altitude in km: '); if (H <= 11)&&(H > 0) kt = -6.5; Tb = 288.15; Hb = 0; T = kt*(H-Hb)+Tb; %%Temp Pb = rho_o*R*Tb; %% base pressure P = Pb*((1+(kt/Tb)*(H-Hb))^-((g)/(R*kt))); %% Pressure c = sqrt(R*T*G); %% Speed of sound rho = P/(R*T); %% Density
delta = P/Pb; %%pressure ratio
theta = (Tb/To)*(1+(kt/Tb)*(H-Hb)); %%Temp ratio
sigma = delta/theta; %%density ratio
fprintf('delta = %f\n theta = %f\n sigma = %f\n P = %f Pa\n T = %f K\n rho = %f kg/m^3',delta, theta, sigma, P, T, rho) %%output pressure, temp.,density & ratios for all three also
……………
else %some other code for imperial units or conversion that isn't written yet

回答(2 个)

Bob Thompson
Bob Thompson 2018-8-27
I usually make this kind of check with a string comparison as the if condition.
if strcmp(input,'SI')==1;
...
else
...
end
Any time you want to run an if statement on a string though it has to be Exactly what you're looking for. Any extra spaces or characters will return a negative comparison with strcmp. I would suggest telling the user what inputs are expected to help them avoid this.
  2 个评论
Ikenna Okoye
Ikenna Okoye 2018-8-27
Ok thanks. I now got this error:
Error using input Not enough input arguments.
Error in Atmosphereto100km (line 4) if strcmp(input,'SI')==1
Stephen23
Stephen23 2018-8-28
编辑:Stephen23 2018-8-28
Do NOT use input as a variable name! This shadows the inbuilt input fucntion.
You need to use the name of the variable, not the name of the input function:
H = input('Enter the word SI or imperial for Temp., pressure and density units','s');
if strcmp(H,'SI') % ==1 is not required
...
else
...
end

请先登录,再进行评论。


Saifur Rahman Opu
Saifur Rahman Opu 2022-11-27
编辑:Saifur Rahman Opu 2022-11-27
Write a program main_1.m which will ask the user the input “Select the shape”. The user has the option of typing ‘square’, ‘circle’, ‘rectangle’. Once the user selects the shape, display a message: “The user has selected : shape”. If the user enters any other shape, it should display an error message “Shape is not valid”.
this is my answer
s=input('select the shape')
square='square';
circle='circle';
rectangular='rectangular';
if s==circle
disp('the user has selected:circle')
elseif s==square
disp('the user has selected: square')
elseif s==rectangular
disp('the user has selected:rectangular')
else
disp('invalid shape')
end
but it still error

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

产品


版本

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by