Filter löschen
Filter löschen

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

70 Ansichten (letzte 30 Tage)
Mohammed kandeel
Mohammed kandeel am 14 Jan. 2018
Beantwortet: Star Strider am 14 Jan. 2018
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

Antworten (2)

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

Kategorien

Mehr zu Characters and Strings finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by