How do i accept only numbers as input to forward to a switch case ?

velocity = input('Enter velocity : ');
switch velocity
case (6.05)
[value]=experiment100(velocity);
case (4.09)
[value]=experiment100(velocity);
case (7.30)
[value]=experiment100(velocity);
case (5.67)
[value]=experiment100(velocity);
case (1.83)
[value]=experiment100(velocity);
case (3.78)
[value]=experiment100(velocity);
case (4.59)
[value]=experiment100(velocity);
case (2.96)
[value]=experiment100(velocity);
case('isempty')
disp('Enter a number')
case('isletter')
disp('please enter a number')
otherwise
disp('please enter a number')
end
This is my code where experiment100 is a function file.The function file only runs with numbers as input.When i press just the enter key byitself it gives me an error and when i input any alphabets or words it gives me an error as well.Im not sure how exactly to fix this.Iv been searching online for a few hours now.If anyone can give me an idea about this it would be great.

 Respuesta aceptada

Adam Danz
Adam Danz el 13 de Nov. de 2018
Editada: Adam Danz el 13 de Nov. de 2018
This should solve both problems. Replace your first line with this.
velocity = [];
while isempty(velocity)
try
velocity = input('Enter velocity : ');
catch
%do nothing
end
end
However, why use a swtich case for this at all? Why not just enter the user input directly into the function?
velocity = input('Enter velocity : ')
[value] = experiment100(velocity);
If you're trying to control which values the user may select,
acceptedVals = [6.05 4.09 7.30 5.67 1.83 3.78 4.59 2.96];
velocity = input('Enter velocity : ')
if ~ismember(velocity, acceptedVals)
error('Selection is limited to [%s]', num2str(acceptedVals))
end

4 comentarios

Thank you.I tried using the way you suggested but i couldnt make it work.I am very new coding my apologies.This fixed my issue.Yes im trying to limit the user's input choices.I modified the code to whats written below.I get the disp shown under catch when i enter a letter and number mixed together .I cant figure out where to add the disp message for only letters or words.Now when i enter a letter or a word ,It keeps asking me for an input till a number is entered.Do you by any chance know which part i should modify to add a disp command saying 'enter a number' before the program asks for the input again in case of letters or words?
velocity = [];
while isempty(velocity)
try
velocity = input('C.Enter velocity in m/s :');
catch
disp('Error:Please enter a number ')
end
end
switch velocity
case (6.05)
[value]=experiment100(velocity);
case (4.09)
[value]=experiment100(velocity);
case (7.30)
[value]=experiment100(velocity);
case (5.67)
[value]=experiment100(velocity);
case (1.83)
[value]=experiment100(velocity);
case (3.78)
[value]=experiment100(velocity);
case (4.59)
[value]=experiment100(velocity);
case (2.96)
[value]=experiment100(velocity);
otherwise
disp('Error||The value entered is not from the table||')
end
When entering letters that aren't in quotes, Matlab assumes it's a variable or a function and if there is no variable or function with that name, it will create an error "Undefined function or variable ". The try/catch block seems to ignore that error and I don't think there's a solution for that. Why do you need to display a message? The input instructions are clear "Enter velocity in m/s :" .
The switch/case is a poor choice. I highly recommend using the ismember() method I suggested above. Below is an updated version I recommend you use. The user must enter one of the values listed. Otherwise there's an error.
acceptedVals = [6.05 4.09 7.30 5.67 1.83 3.78 4.59 2.96];
velocity = [];
while isempty(velocity)
velocity = input('Enter velocity : ');
end
if ~ismember(velocity, acceptedVals)
error('Selection is limited to [%s]', sprintf('%.2f ', acceptedVals))
end
That makes sense.Now i have a better understanding of why the error kept popping up.The modified code which you wrote works for me.Il move my future codes to ismember for simplicity.Thank you so much for your help
To work around the problem of the user potentially entering text, you would use the 's' option of input()
velocity = nan;
while isnan(velocity)
velocity = str2double( input('C.Enter velocity in m/s :', 's') );
end

Iniciar sesión para comentar.

Más respuestas (2)

Walter Roberson
Walter Roberson el 13 de Nov. de 2018
For limiting choices use menu() or aa drop down menu or the like.
Do not use ismember with floating point numbers: use ismembertol.

2 comentarios

@Nabeel N, these are better suggestions. Here's what it would look like implemented. Try running the code.
acceptedVals = [6.05 4.09 7.30 5.67 1.83 3.78 4.59 2.96];
selection = menu('Select velocity', strsplit(num2str(acceptedVals)));
[value] = experiment100(acceptedVals(selection));
Thank you for taking time to look into this.
The code works perfectly
Il change my original code to this.

Iniciar sesión para comentar.

Luna
Luna el 13 de Nov. de 2018
Editada: Luna el 13 de Nov. de 2018
Hi Nabeel,
You can check that after getting the velocity and before your switch by using a flag isnumeric(velocity). It returns logical 1 if your velocity is numeric and returns logical zero.
try like below:
if isnumeric(velocity)
switch velocity
...
end
else
%do what you want
end

2 comentarios

This doesn't solve the problem of entering text as if it were a variable that doesn't exist. For example, ...
Enter velocity: fubar
...would cause an error if fubar isn't a varible in the workspace.
This also doesn't address the problem caused when enter is pressed while the input is blank.
Your solution does address the problem of entering a string, though.
Thank you for your quick answer.Yeah i tried this but entering text caused an error

Iniciar sesión para comentar.

Categorías

Más información sobre Scripts en Centro de ayuda y File Exchange.

Productos

Versión

R2018b

Etiquetas

Preguntada:

N V
el 13 de Nov. de 2018

Comentada:

el 13 de Nov. de 2018

Community Treasure Hunt

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

Start Hunting!

Translated by