How do I Take in input string and insert that string into an if statment
5 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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 comentario
Stephen23
el 28 de Ag. de 2018
Do NOT use input as a variable name! Then you shadow the input command.
Respuestas (2)
Bob Thompson
el 27 de Ag. de 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 comentarios
Stephen23
el 28 de Ag. de 2018
Editada: Stephen23
el 28 de Ag. de 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
Saifur Rahman Opu
el 27 de Nov. de 2022
Editada: Saifur Rahman Opu
el 27 de Nov. de 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
0 comentarios
Ver también
Categorías
Más información sobre Loops and Conditional Statements en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!