stack_underflow
stack_underflow

Reputation: 41

Matlab ask for integer loop

I'm creating a program to simulate a random walk and it requires the user to input an integer number of steps to take for the walk.

The prompt for this uses code very similar to this:

    **% Ask user for a number.
    defaultValue = 45;
    titleBar = 'Enter a value';
    userPrompt = 'Enter the integer';
    caUserInput = inputdlg(userPrompt, titleBar, 1,{num2str(defaultValue)});
    if isempty(caUserInput),return,end; % Bail out if they clicked Cancel.
    % Round to nearest integer in case they entered a floating point number.
    integerValue = round(str2double(cell2mat(caUserInput)));
    % Check for a valid integer.
    if isnan(integerValue)
    % They didn't enter a number.  
    % They clicked Cancel, or entered a character, symbols, or something else not allowed.
    integerValue = defaultValue;
    message = sprintf('I said it had to be an integer.\nI will use %d and continue.', integerValue);
    uiwait(warndlg(message));
    end**

However, I want it to simply display the "Enter a value" prompt again if the user does not enter an integer the first time i.e. 4.4.

Any ideas?

Thanks!

Upvotes: 0

Views: 81

Answers (3)

NKN
NKN

Reputation: 6424

Remember that unspecified inputs in MATLAB are double-precision by default. For instance a=3 is not an integer. So you should consider two cases:


Integer type

If you are talking about integer type in MATLAB the easiest way is to use isinteger function by MATLAB:

tf = isinteger(A)

for instance:

isinteger(4.4)
=
    0

as I mentioned before, 3 is not an integer:

isinteger(3)
=
    0

but this one is integer actually:

isinteger(uint8(3))
=
    1

To repeat the input query also easily use the same function in a while loop

while ~isinteger(a)
    disp('enter an integer');
    ....
end

Constant double-precision with no decimal

But if you are considering normal constant inputs to be integers you could convert them to integer and compare the result with the original value:

while a ~= double(int64(a))
    disp('enter an integer');
    ....
end

int64 converts the double type to integer, and double converts it back to double. If in this process the number remains unchanged, then you could consider that it was intended to be an integer.


Recommendation for you specific program

I would use a fix function to get rid of the decimal parts. Usually when you receive a double-precision number including decimal values, the main intention is the the number before the floating point. So in many algorithms it is common practice to use fix to round each element of the given number to the nearest integer toward zero.

Upvotes: 1

Finn
Finn

Reputation: 2343

The first answer is totally correct for checking for an integer value, but to address the "show prompt again" issue you can just use a loop conditioning it to get the exact kind of data you want:

caUserInput = nan; %or anything worng for that matter
while isempty(caUserInput) || isnan(caUserInput)
    caUserInput = inputdlg(userPrompt, titleBar, 1,{num2str(defaultValue)});
end 

if you want you can start it again with different argument lines in a more fancy style:

inputiswrong = 1; %or anything worng for that matter
while inputiswrong 
    inputiswrong = 0;
    caUserInput = inputdlg(userPrompt, titleBar, 1,{num2str(defaultValue)});
   if isempty(caUserInput )
      userPrompt = 'Try again with an input';
      inputiswrong = 1;
   end
   if isnan(caUserInput )
      userPrompt = 'not really a number';
      inputiswrong = 1;
   end
   %and so on
end 

In both scenarios you should consider transforming the caUserInput to something you could use, i think inputdlg returns a cell so maybe a cell2mat() around the inputdlg().

Upvotes: 1

hiandbaii
hiandbaii

Reputation: 1331

if (mod(integerValue,1) == 0)

will evaluate to true if integerValue is an integer. Simply augment your if statement w/ this logic. You might want to consider changing to using a while loop so the user can enter bad input more than once.

Upvotes: 2

Related Questions