Reputation: 630
I'm writing a simple program in Matlab and am wondering the best way to ensure that the value a user is inputting is a proper integer.
I'm currently using this:
while((num_dice < 1) || isempty(num_dice))
num_dice = input('Enter the number of dice to roll: ');
end
However I really know there must be a better way, because this doesn't work all the time. I would also like to add error checking ala a try catch block. I'm brand new to Matlab so any input on this would be great.
EDIT2:
try
while(~isinteger(num_dice) || (num_dice < 1))
num_dice = sscanf(input('Enter the number of dice to roll: ', 's'), '%d');
end
while(~isinteger(faces) || (faces < 1))
faces = sscanf(input('Enter the number of faces each die has: ', 's'), '%d');
end
while(~isinteger(rolls) || (rolls < 1))
rolls = sscanf(input('Enter the number of trials: ', 's'), '%d');
end
catch
disp('Invalid number!')
end
This seems to be working. Is there anything noticeably wrong with this? isinteger is defined by the accepted answer
Upvotes: 5
Views: 22346
Reputation: 125874
One easy way to check the properties of function/user-supplied inputs is to use the validateattributes
function. I don't know when this function was first introduced; it may not have been present when the question was first asked, but I think it bears mentioning even though the question is older.
If you want to check that a user-supplied input is a scalar, positive, non-zero, real-valued integer of any numeric data type, you can do so using a try-catch block like so:
invalidInput = true;
while invalidInput
num_dice = input('Enter the number of dice to roll: ');
try
validateattributes(num_dice, {'numeric'}, ...
{'scalar', 'integer', 'real', 'finite', 'positive'})
invalidInput = false;
catch
disp('Invalid input. Please reenter...');
end
end
If you're dealing with function inputs instead of a user-supplied input, you could also make use of the inputParser
class.
Upvotes: 3
Reputation: 1
Try this one. I think it is simpler :)
% Assume false input at the beginning
checkDice = 0;
% As long as the input is false, stay in the loop
while ~checkDice
% Take the input as a string
strDice = input('Enter the number of dice to roll: ', 's');
% Convert string to number
dice = str2num(strDice);
% Length of dice will be 1 iff input is a single number
if length(dice) ~=1
checkDice = 0;
else
% Search for a positive integer
checkDice = ((dice>=1) && dice == floor(dice));
end
end
Upvotes: -1
Reputation: 7175
The following can be used directly in your code and checks against non-integer input including empty, infinite and imaginary values:
isInteger = ~isempty(num_dice) ...
&& isnumeric(num_dice) ...
&& isreal(num_dice) ...
&& isfinite(num_dice) ...
&& (num_dice == fix(num_dice));
The above will only work correctly for scalar input. To test whether a multi-dimensional array contains only integers you can use:
isInteger = ~isempty(x) ...
&& isnumeric(x) ...
&& isreal(x) ...
&& all(isfinite(x)) ...
&& all(x == fix(x))
EDIT
These test for any integer values. To restrict the valid values to positive integers add a num_dice > 0
as in @MajorApus's answer.
You can use the above to force the user to input an integer by looping until they succumb to your demands:
while ~(~isempty(num_dice) ...
&& isnumeric(num_dice) ...
&& isreal(num_dice) ...
&& isfinite(num_dice) ...
&& (num_dice == fix(num_dice)) ...
&& (num_dice > 0))
num_dice = input('Enter the number of dice to roll: ');
end
Upvotes: 7
Reputation: 2404
Try this, modify it as needed.
function answer = isint(n)
if size(n) == [1 1]
answer = isreal(n) && isnumeric(n) && round(n) == n && n >0;
else
answer = false;
end
Upvotes: 6
Reputation: 10381
Take the input as a string and use sscanf(http://www.mathworks.com/help/techdoc/ref/sscanf.html) to determine if a valid integer was converted from the text.
Upvotes: 0