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

caseinputMATLABswitch

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.

Best Answer

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
Related Question