Borrar filtros
Borrar filtros

if else statement with strings

8 visualizaciones (últimos 30 días)
Badr Al-Sabri
Badr Al-Sabri el 7 de Mayo de 2022
Comentada: Riccardo Scorretti el 7 de Mayo de 2022
I want to take the user input and check if it is:
if the user input 'high pass' -> calcute this equation:GpRC = tf(R*C,1 + R*C);
if the user input 'low pass' --> calcute this equation:GpRC = tf(1,1 + R*C);
and then plot the true statement
this is the code:
%Make the user choose the filter
msgout = 'Choose from (high pass / low pass) \n';
%Calculate the gain
filter_option = input(msgout);
if strcmp(filter_option,'high pass')
GpRC = tf(R*C,1 + R*C);
plot(f_c,GpRC)
elseif strcmp(filter_option,'low pass')
GpRC = tf(1,1 + R*C);
plot(f_c,GpRC)
else
disp('Error!')
end
  3 comentarios
Dyuman Joshi
Dyuman Joshi el 7 de Mayo de 2022
What exactly is the problem you are facing?
Badr Al-Sabri
Badr Al-Sabri el 7 de Mayo de 2022
--> msgout = 'Choose from (high pass / low pass) \n';
When I write "high pass"
I am getting an error

Iniciar sesión para comentar.

Respuesta aceptada

Riccardo Scorretti
Riccardo Scorretti el 7 de Mayo de 2022
That's being said, your problem is (most probably) that you wish to ask the user to enter a string:
filter_option = input(msgout);
In this case, you must add the second argument 's' to input, otherwise input will look for a numerical value or a numerical expression:
filter_option = input(msgout, 's'); % *** add 's' as second argument ***
  2 comentarios
Badr Al-Sabri
Badr Al-Sabri el 7 de Mayo de 2022
Thank you
It worked
Riccardo Scorretti
Riccardo Scorretti el 7 de Mayo de 2022
You are welcome

Iniciar sesión para comentar.

Más respuestas (0)

Community Treasure Hunt

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

Start Hunting!

Translated by