0
votes

I'm creating a function in MatLab that prompts for user input and then displays a message depending on that input. A valid input (an integer from 1-10) will show a message of 'Input recorded' (this is working), and any other input (ie. non integer values, integers outside the range, or text) should display 'Invalid input, try again', and then prompt the user for an input again. This is the function as it stands:

n = 0;
while n == 0
    userInput = input('Input an integer from 1 - 10\n');
    
    switch userInput
        case {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
          disp('Input recorded');
          n = 1;
        otherwise
          disp('Invalid input, try again')
          pause(2);
          n = 0;
    end
end

At the moment, an integer input within the range triggers the confirmation message and breaks out of the while loop, and any other numeric input triggers my 'Invalid input' message and cycles round again, however, a text input (eg "five") gives me a MatLab error message (image of my console window output included). How do I let the switch statement also accept a text input, to avoid breaking my code if the user types text?

Obviously I'm using a switch for this at the moment, but I'm sure there might be a better solution with an if, elseif statement - I'm open to any solution that gets the job done!

1
Should five be a valid input?Sardar Usama
An input like "five" or 'five' won't return any error but five will return an error if you don't have a variable named five in your workspace.Sardar Usama
@SardarUsama Unless you tell MATLAB to read the input as string with input('Input an integer from 1 - 10\n','s');. But first Robert should answer your question if only numbers like 5 are allowed as input or even five.Till
hey guys, so nothing other than integers from 1 to 10 should be a valid input. They should only be valid in integer form (not text), so "five" etc shouldn't be a valid input. I could try accepting the input as a string as @SardarUsama says, and then use str2num to convert it to an int (as i need to use this value to select an array element)?Robert Reid
I've edited the function to accept the input as a string, and then used str2num to convert this to an int afterwards. This makes the various messages display as desired, and ensures the programme only accepts the inputs I want. Thanks for your help guysRobert Reid

1 Answers

1
votes

If you use input with just one parameter, MATLAB tries to evaluate any expression in the input. So if someone gives an input like e.g. 'a', MATLAB will search for 'a' in the variables of your current workspace. Since there's no defined variable with that name, MATLAB will throw an exception. That's why you have to specify the format in input as second parameter. I'd suggest you to define the specify the format string, then the input can be anything. Then convert the string to a double number and start checking the input:

n = 0;
while n == 0
    userInput = input('Input an integer from 1 - 10\n','s'); %Read input and return it as MATLAB string
    userInput = str2double(userInput);
    
    if (~isnan(userInput) && ...               %Conversion didn't fail AND
        mod(userInput,1) == 0 &&...            %Input is an integer AND
        userInput >= 1 && userInput <= 10)     %Input is between 1 AND 10
        
        disp('Input recorded');
        n = 1;

    else   
     
        disp('Invalid input, try again')
        pause(2);
        n = 0;

    end
end