How to check user input data using letters
20 views (last 30 days)
Show older comments
I am trying to create a function that asks a user to enter a capital letter A through J. if the letter is lowercase or from K-Z, I need to prompt the user to enter a valid letter. My code so far
function HumanInputFxn(Letter_Input)
Good=0;%initilizes loop variable
while Good<1
Letter_Input=input('Enter capital letter from A to J:'); %allows user to enter a letter
if Letter_Input = num2cell('a':'z') || Letter_Input = num2cell('K':'Z')
%a lower case letter or uppercase letter above J
fprintf('you entered %c \n please enter a capital, valid letter. \n',Letter_Input);
else %if the input is a valid
fprintf('You have entered %c, a valid letter! You will now be prompted to enter a number.\n',Letter_Input)
Good=1; %stops letter loop
end
end
end
I get errors the following errors:
Unrecognized function or variable 'Letter_Input'.
Error in HumanInputFxn(Letter_Input)
Incorrect use of '=' operator. To assign a value to a variable,
use '='. To compare values for equality, use '=='.
How can I properly define invalid inputs in my if statement? Why is Letter_Input not recognized? thanks
0 Comments
Accepted Answer
Sudhakar Shinde
on 14 Oct 2020
Edited: Sudhakar Shinde
on 14 Oct 2020
you could try this:
function HumanInputFxn(~)
Good=0;%initilizes loop variable
while Good<1
Letter_Input=input('Enter capital letter from A to J: ','s'); %allows user to enter a letter
if any(contains(num2cell('a':'z'),Letter_Input)) || any(contains(num2cell('K':'Z'),Letter_Input))
%a lower case letter or uppercase letter above J
fprintf('you entered %c \n please enter a capital, valid letter. \n',Letter_Input);
else %if the input is a valid
fprintf('You have entered %c, a valid letter! You will now be prompted to enter a number.\n',Letter_Input)
Good=1; %stops letter loop
end
end
end
3 Comments
More Answers (1)
Adam Danz
on 14 Oct 2020
Edited: Adam Danz
on 14 Oct 2020
The input() method of acquiring responses from a user is highly unconstrained and difficult to manage (see this example). What if the user enters more than 1 letter? or a number or a punctuation character? I suggest using a question dialog box or some other constrained user interface.
Nevertheless, here are two tests that are fairly leak-proof to confirm that,
- The user entered 1 and only 1 character
- The input is a character or string
- The character was between A and J
- The character was upper case
Good = false; % initilizes loop variable
while ~Good
Letter_Input=input('Enter capital letter from A to J:','s'); % allows user to enter a letter
isValid = sum(ismember('A':'J',char(upper(Letter_Input))))==1 ...
&& (ischar(Letter_Input) || isstring(Letter_Input)); % test for A:J (ignores case)
caseIsValid = isstrprop(Letter_Input, 'upper'); % tests for case
if ~isValid || ~caseIsValid
%a lower case letter or uppercase letter above J
fprintf('you entered %c \n please enter a capital, valid letter. \n',Letter_Input);
else
fprintf('You have entered %c, a valid letter! You will now be prompted to enter a number.\n',Letter_Input)
Good=1; %stops letter loop
end
end
0 Comments
See Also
Categories
Find more on Cell Arrays in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!