Only gu values tu ?

8 ビュー (過去 30 日間)
Steven Thies
Steven Thies 2021 年 2 月 18 日
コメント済み: Rena Berman 2021 年 5 月 6 日
Hello Matlab community,
i created a script and everything works but i want to add that the script accepts also only integer values for m. How can i do this?
i = 0;
m = input('please number: ','s');
while isnan (str2double(m)) == true || str2double(m)<0
m = input('please again: ','s');
if isnan (str2double(m)) == true || str2double(m)<0
i = i+1;
if i == 4
return
end
else
break
end
end
  2 件のコメント
Stephen23
Stephen23 2021 年 2 月 19 日
編集済み: Stephen23 2021 年 2 月 19 日
Original question by Steveb Thies retrieved from Google Cache:
"Only integer values ?"
Hello Matlab community,
i created a script and everything works but i want to add that the script accepts also only integer values for m. How can i do this?
i = 0;
m = input('please number: ','s');
while isnan (str2double(m)) == true || str2double(m)<0
m = input('please again: ','s');
if isnan (str2double(m)) == true || str2double(m)<0
i = i+1;
if i == 4
return
end
else
break
end
end
Rena Berman
Rena Berman 2021 年 5 月 6 日
(Answers Dev) Restored edit

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

回答 (2 件)

Les Beckham
Les Beckham 2021 年 2 月 19 日
To test for integer values of double numbers (doubles are the default numeric type in Matlab), I would suggest using mod(m,1).
So, in your case, replace "isnan (str2double(m)) == true || str2double(m)<0" with "mod(m,1) ~= 0" to detect a number that is not an integer.
Of course your code does nothing whether the test passes or fails so I'm not sure what the point is. I assume you have left some things out?

Walter Roberson
Walter Roberson 2021 年 2 月 19 日
positive integers have the property that all of their characters are one of '0','1','2','3','4','5','6','7','8','9', or '+'... unless you want to accept exponential notation as well, in which case the characters might also be 'd', 'D', 'e', 'E', '.', or '-' .
  1 件のコメント
Les Beckham
Les Beckham 2021 年 2 月 19 日
Your answer made me notice that Steven was using the 's' option in his input call. I had missed that before.
Perhaps remove the 's' option and use mod as I suggested? Still, the example code does nothing with the result, so it is difficult to tell what the desired behavior is.

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

カテゴリ

Help Center および File ExchangeEnvironment and Settings についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by