how to create an error message when invalid data is input and then prompt to re-enter the data
Show older comments
person_age = input('Please enter persons age')
If person_age < 1
errordlg('Please input valid number','Error')
end
How do I display this error message and then prompt the user to re-enter the person_age?
Many thanks
Accepted Answer
More Answers (1)
Guillaume
on 14 Nov 2019
Typical pattern for this is:
value = someinvalidvalue;
while valueisinvalid
value = input('Enter value');
end
Note that your test is extremely incomplete. Any of the following inputs would be considered valid age:
- NaN
- [-1, 2]
- Inf
- 'abcdef'
- 5 + 1i*2
You may want to change your test to:
while isnumeric(person_age) || isscalar(person_age) || isreal(person_age) || isfinite(person_age) || person_age < 1
1 Comment
Eddie Burns
on 14 Nov 2019
Categories
Find more on Naming Conventions in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!