필터 지우기
필터 지우기

How to check user input data using letters

조회 수: 58 (최근 30일)
scobug411
scobug411 2020년 10월 14일
편집: Adam Danz 2020년 10월 14일
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

채택된 답변

Sudhakar Shinde
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
Adam Danz
Adam Danz 2020년 10월 14일
try these inputs,
  • 'ABC'
  • 1
Sudhakar Shinde
Sudhakar Shinde 2020년 10월 14일
Welcome.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Adam Danz
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

카테고리

Help CenterFile Exchange에서 Dialog Boxes에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by