1

How do I in MatLab catch the error that occours when the user enters letters and other things that aren't numbers in the input:

width = input('Enter a width: ');

I have played around for a while with the try/catch command:

width = 0;
message = '';

% Prompting.
while strcmp(message,'Invalid.') || width < 1 || width ~= int32(width)

  try
     disp(message)
     width = input('Frame width: ');
  catch error
     message = 'Invalid.';
  end

end

But with no luck (the above doesn't work). As shown I would like a simple message like "Frame width: " for the user the first time he has to enter his choice. But if an error is caught I want the message for him to be "Invalid. Try again: " fx everytime an error occours.

I have also tried the error() but I don't know how to place that correctly. Since the error() doesn't take the input command, where the error happends, as an argument, it must detect it in another way, which I can't figure.

Any help would be appreciated.

2 Answers 2

4
width = input('Frame width: ');
while(~isInt(width))
    width = input('Invalid. Try again: ');
end

and you'll have to have the following function somewhere (or another implementation of it)

function retval = isInt(val)
    retval = isscalar(val) && isnumeric(val) && isreal(val) && isfinite(val) && (val == fix(val));
end
Sign up to request clarification or add additional context in comments.

8 Comments

+1. I'd note that you could just toss an isscalar in there rather than the if/else statement.
It doesn't work when I enter a letter... The error happens at the line with width = input('Frame width: ');. It seems that when a letter (or any string) is entered, an error happens here because the input() did not ask for a string. Then the program never reaches the while loop. Instead it breaks at this line.
@Steeven If you enter a string with ' ' around it, what happens? If you don't enclose the string, it will think you're trying to enter a variable from the workspace and evaluates that variable. If an error occurs, it should just prompt again though rather than breaking entirely
So, if I want to avoid the 's' argument to the input(), I have to add apostrophes around the input string? That wouldn't be user friendly for the user. Is it then not possible to catch the error that occours because a string is sent?
only apostrophes for strings, just like in your normal code: 4 is integer, 4.5 is double, a is a variable, 'b' is a string. You can always use the 's' parameter to define that your input is always a string.. but since you're avoiding that, I guess we're stuck..
|
2
answer = input('Frame width: ', 's');
[width, status] = str2num(answer);
while ~status || ~isscalar(width) || width ~= floor(width)
  answer = input('Invalid. Try again: ', 's');
  [width, status] = str2num(answer);
end
disp(width);

(status is 0 if the conversion failed. Without the isscalar test, an input like [1 2; 3 4] would also be accepted. The last test ensures that width must be an integer.)

4 Comments

You may also want to make sure that fractional parts aren't allowed (the title suggests integer inputs are desired).
Unfortunately I need to avoid using the 's' argument in the input() command. I am looking for a solution to catch the error when this argument is not used.
Interesting. Why do you need to avoid the 's'? It's needed to handle the input as a string. It's a usual practice to receive user input as a string, and try to convert it and check the result of conversion in a second step.
I have to give in it seems. I am convinced. Your answer did the trick, when the 's' couldn't be avoided.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.