MATLAB: How to test if an input value is an integer

integer

I want to know how to test whether an input value is an integer or not. I have tried using the function isinteger, but I obtain, for example, isinteger(3) = 0. Apparently, any constant is double-precision by Matlab default, and is therefore not recognized as an integer. Can anyone tell me what function I'm supposed to use?

Best Answer

E.g.,
>> isaninteger = @(x)isfinite(x) & x==floor(x)
isaninteger =
@(x)isfinite(x) & x==floor(x)
>> isaninteger(3)
ans =
1
>> isaninteger(3.1)
ans =
0
>> isaninteger(inf)
ans =
0
>> isaninteger(nan)
ans =
0
>> isaninteger(uint8(4))
ans =
1
>> isaninteger([-inf -1.2 0 1.3 5 inf nan])
ans =
0 0 1 0 1 0 0