Clear Filters
Clear Filters

How to check the input from user is "positive integer Number" ?

49 views (last 30 days)
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

Answers (2)

Star Strider
Star Strider on 14 Jan 2018
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 on 14 Jan 2018

Categories

Find more on Numeric Types in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!