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

5 Ansichten (letzte 30 Tage)
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

Antworten (2)

Bob Thompson
Bob Thompson am 27 Aug. 2018
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 Kommentare
Ikenna Okoye
Ikenna Okoye am 27 Aug. 2018
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 am 28 Aug. 2018
Bearbeitet: Stephen23 am 28 Aug. 2018
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

Melden Sie sich an, um zu kommentieren.


Saifur Rahman Opu
Saifur Rahman Opu am 27 Nov. 2022
Bearbeitet: Saifur Rahman Opu am 27 Nov. 2022
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

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Produkte


Version

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by