Azzera filtri
Azzera filtri

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

49 visualizzazioni (ultimi 30 giorni)
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

Risposte (2)

Star Strider
Star Strider il 14 Gen 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 il 14 Gen 2018

Categorie

Scopri di più su Numeric Types in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by