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

回答(2 个)

Star Strider
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

Rik
Rik 2018-1-14

类别

Help CenterFile Exchange 中查找有关 Characters and Strings 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by