How to check the input from user is "positive integer Number" ?
84 次查看(过去 30 天)
显示 更早的评论
I will receive input form user and i need to make sure the entered data is a number ( integer Only )
If the user enter string or char between numbers or negative number or special character.
I need to know the function in these above cases.
Thanks
0 个评论
回答(2 个)
Star Strider
2018-1-14
It is straightforward to write a simple anonymous function that will return 1 (true) when those conditions are met:
int_gt_0 = @(n) (rem(n,1) == 0) & (n > 0); % Returns 1 For Integers Greater Than 0
v = [-2 -1 -0.9 0 0.9 1 2] % Test Arguments
result = int_gt_0(v) % Test The Function
produces:
v =
-2 -1 -0.9 0 0.9 1 2
result =
0 0 0 0 0 1 1
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Characters and Strings 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!