How to limit user input to only one character?

3 ビュー (過去 30 日間)
Maureen O'Brien
Maureen O'Brien 2016 年 11 月 30 日
コメント済み: Image Analyst 2016 年 11 月 30 日
I'm trying to figure out a way to limit user input to only one character and makes sure that its a letter. I figured out that to make sure that it is a capital letter I can use if all(ismember(variablename,65:90)), but I dont know how to make matlab only accept one character as a user input. Thanks, in advance, for the help.

回答 (2 件)

Image Analyst
Image Analyst 2016 年 11 月 30 日
Try casting to upper and taking only the first character
variablename = upper(variablename); % Cast to upper case.
variablename = variablename(1); % Extract first character only.
if ~isletter(variablename)
message = sprintf('You must enter a single upper case character!\nTry replacing the user and try again!');
uiwait(warndlg(message));
end
  2 件のコメント
Maureen O'Brien
Maureen O'Brien 2016 年 11 月 30 日
The more I think about it, I want to make a function that will check to make sure the input is only one character. So while I appreciate the idea, that's not exactly the way that I want to go about it.
Image Analyst
Image Analyst 2016 年 11 月 30 日
Then use length():
if length(variablename) > 1
% User entered a response that was too long.

サインインしてコメントする。


James Tursa
James Tursa 2016 年 11 月 30 日
Maybe this FEX submission by Jos is close to what you want:

カテゴリ

Help Center および File ExchangeAnnotations についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by