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

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

댓글 수: 1

Do NOT use input as a variable name! Then you shadow the input command.

댓글을 달려면 로그인하십시오.

답변 (2개)

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

Ok thanks. I now got this error:
Error using input Not enough input arguments.
Error in Atmosphereto100km (line 4) if strcmp(input,'SI')==1
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일

0 개 추천

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

카테고리

도움말 센터File Exchange에서 Characters and Strings에 대해 자세히 알아보기

제품

릴리스

R2018a

질문:

2018년 8월 27일

편집:

2022년 11월 27일

Community Treasure Hunt

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

Start Hunting!

Translated by