Best Practice for Testing for Appropriate Numerical Input

2 views (last 30 days)
Hi Y'all,
I'm working on a GUIDE GUI and I can't come up with a satisfactory was to test edit box input. Suppose I want to get a single double value entered by the user. My current approach is along the lines of:
input = str2double(get(hObject,'string'));
if(~isnan(input))
handles.x = input;
end
I realized this was unsatisfactory because if the user enters 'inf' or 'i' the statement evaluates as true.
isa(input,'double')
Doesn't work either for the same reason.
Is there a nice short single evaluation I can do to test for a regular double excluding irrationals, infinity, etc?
Is testing to see if the input falls within a range my only choice? Is str2double a poor choice?
  4 Comments
dpb
dpb on 5 Jun 2015
Well, there are ways in which you have a callback on every keystroke and confirm that it's a valid character entered on each keystroke--but there again, just because entered a valid character, that does not necessarily mean the end string will be properly formed number; that requires syntax checking at the same time. What interfaces I've seen that tried to do that too excessively tended to be a real pit(proverbial)a(ppendage) for the user owing to the unexpected interactions that could occur. If all it did was prevent entering a 'X', that wasn't so bad, but if it thought there was an issue, it could be come difficult to get past it w/o essentially just starting over. Think of all the possible ways one can enter a given floating point number and the permutations on order of digits, punctuation, decimal points, etc., etc., etc., ... You're in essence having to rewrite the input parser for the i/o input library.
John Petty
John Petty on 5 Jun 2015
I think the problem is that I'm just not used to working with doubles as what the users are entering for input. It's just weird adjusting to tests for NaN and inf in floating point in an input scenario when I've never had to do something equivalent in say C.

Sign in to comment.

Accepted Answer

dpb
dpb on 5 Jun 2015
*isfinite* removes the NaN and Inf possibilities
*isreal* tests whether is a complex nonzero imaginary part
  1 Comment
John Petty
John Petty on 5 Jun 2015
Are there any other corner cases one would need to address if testing individual properties like that?
I'm almost tempted to just see if the input contains a character other than a number or '-' instead and then just assume the str2double result is kosher.

Sign in to comment.

More Answers (1)

Sean de Wolski
Sean de Wolski on 5 Jun 2015
If you're willing to programmatically add a new edit box, you could use my FEX:numericEditbox to avoid reinventing the wheel. It also has options for range and constraining to integers.
In the Output function you would just have:
h = numericEditbox(etc)
handles.neditbox = h;
guidata(handles.figure1,handles)
Then query it like any other control.

Tags

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!