How to limit user input to only one character?
5 次查看(过去 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.
0 个评论
回答(2 个)
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 个评论
Image Analyst
2016-11-30
Then use length():
if length(variablename) > 1
% User entered a response that was too long.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Annotations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!