Asking for user input of numbers and letters.

21 ビュー (過去 30 日間)
Thomas Robertson
Thomas Robertson 2022 年 3 月 3 日
コメント済み: David Hill 2022 年 3 月 3 日
Hi, I coded a program that can can take an image of a lisence palte and output it's numbers and letters. Now I'm trying to set up a way for the user to enter the correct lettes/numbers of the lisence plate to see if it matches with the letters and numbers generated by MatLab I'm just not sure how. I'm not sure how the user can input a line of both numbers and letters as input. For reference noPlate is the name of the numberplate generated by matlab.
x = input('What is the correct value of the lisence plate? ')
if (x==noPlate)
output('Nice')
else
output('The entered value does not match MatLab. Would you like to try again? (Y/N)')
end
Thanks, any help will be appreciated!
  1 件のコメント
David Hill
David Hill 2022 年 3 月 3 日
Is noPlate a character array (exampple 'F4RE9')? If not it would be easier to convert noPlate to a character array.
if isequal(x,noPlate) %should use isequal
You could convert 'F4RE9' to both letters and numbers in a cell array
x= 'F4RE9';
a=mat2cell(x',ones(size(x')));
for k=1:length(a)
if a{k}>='0'&&a{k}<='9'
a{k}=str2double(a{k});
end
end
%now a is a cell array of letters and numbers and isequal will still work
%as long as noPlate is in same cell array

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

回答 (1 件)

Abolfazl Chaman Motlagh
Abolfazl Chaman Motlagh 2022 年 3 月 3 日
if the plate (variable) could have both number and latters, deal with them like string. for getting input as string you need extra option 's' . for comparing two string use strcmp.
User_input = input('What is the correct value of the lisence plate? ','s');
if strcmp(User_input,noPlate)
output('Nice')
else
output('The entered value does not match MatLab. Would you like to try again? (Y/N)')
end
noPlate should be string too. like:
noPlate = '123J45';

カテゴリ

Help Center および File ExchangeData Type Conversion についてさらに検索

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by