how to only have a certain amount of numbers in an input of as many characters and number that the user wants

2 ビュー (過去 30 日間)
prompt = 'Enter atleast 10 numbers: ';
str=0;
str = input(prompt,'s');
while length(str)~=10
str = input(prompt,'s');
end
that is the code that I'm using right now but I want the user to be able to input as many characters as they want but they can only enter 10 numbers. For example the user should be able to input:123we45ru687u90 but as long as it has ten numbers, the code will continue, no matter the amount of characters. Thank you!
  1 件のコメント
KSSV
KSSV 2016 年 10 月 25 日
You remove the while condition. Simply use
prompt = 'Enter atleast 10 numbers: ';
str = input(prompt,'s');

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

回答 (1 件)

John Wirzburger
John Wirzburger 2016 年 10 月 25 日
If they must enter exactly 10 numbers and any number of characters, I would change your code to
prompt = 'Enter at least 10 numbers: ';
str='0';
while length(regexp(str,'\d'))~=10
str = input(prompt,'s');
end

カテゴリ

Help Center および File ExchangeSignal Generation and Preprocessing についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by