MATLAB: How to use if statement to test if it is a integer and real number

if statement

x = input('enter the number: ');
if isreal(x)
else
delete x
if isinteger(x)
delete x
end
end
I try to do it, but it doesn't work.How to make it? It just a part of the whole script. It requires to delete the number if it isn't a integer or real number.

Best Answer

MATLAB has a special integer class. (See the documentation for isinteger for details.) The numeric output x of your input statement will always return a double in x, so the isinteger call will always fail.
If you want to test for a whole number, use the rem function.
Here is one way to implement your if block:
if isreal(x) && rem(x,1)==0
else
x = [];
end
x
The first line tests if x is real, then if it is, goes on to see if dividing it by 1 leaves a remainder. (The ‘&&’ operator will only evaluate the second part of the statement if the first part is true.) If both conditions in the first line are true, it leaves x alone. If one or the other condition fails, that means x is not real or x is not a whole number, and sets x to the empty matrix, deleting it from the workspace.