How to check user input data using letters
20 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Sudhakar Shinde
2020-10-14
编辑:Sudhakar Shinde
2020-10-14
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 个评论
更多回答(1 个)
Adam Danz
2020-10-14
编辑:Adam Danz
2020-10-14
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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Cell Arrays 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!