Best Practice for Testing for Appropriate Numerical Input

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 件のコメント

dpb
dpb 2015 年 6 月 5 日
There's more than that possible that's still valid... "The string may contain digits, a comma (thousands separator), a decimal point, a leading + or - sign, an 'e' preceding a power of 10 scale factor, and an 'i' for a complex unit." And, while not mentioned explicitly, there's at minimum the 'E' variant for 'e'. I'm not sure w/o testing whether may/may not handle the Fortran-style 'd/D' double precision exponential character or not. Also, one can have a malformed string that consists of only the allowable characters so just testing membership in the club isn't sufficient.
All in all, you can presume it was not valid if strdouble returns NaN, but probably more overall effort in testing the string than verifying the result.
There's always the route of a small helper function if you're going to be doing this in many locations; if it's just a single entry box, a small logic expression ain't all that bad...
isOK=isfinite(x) & isreal(x)
OTOMH w/ your expressed want don't think there's anything else--of course, this doesn't prevent user from entering something from the range of realmin to realmax but that's a different question than whether it's a valid double or not. There, indeed, you're to the point of limiting a value.
If that's the case of there being some allowable range, there's always using a slider or other input mechanism than an edit box.
John Petty
John Petty 2015 年 6 月 5 日
Huh- it seems like isn't a more elegant way to do it then and testing valid input will just require a number of checks. Thanks for your help!
dpb
dpb 2015 年 6 月 5 日
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 2015 年 6 月 5 日
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.

サインインしてコメントする。

 採用された回答

dpb
dpb 2015 年 6 月 5 日

0 投票

*isfinite* removes the NaN and Inf possibilities
*isreal* tests whether is a complex nonzero imaginary part

1 件のコメント

John Petty
John Petty 2015 年 6 月 5 日
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.

サインインしてコメントする。

その他の回答 (1 件)

Sean de Wolski
Sean de Wolski 2015 年 6 月 5 日

0 投票

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.

カテゴリ

ヘルプ センター および File ExchangeCharacters and Strings についてさらに検索

製品

タグ

質問済み:

2015 年 6 月 5 日

回答済み:

2015 年 6 月 5 日

Community Treasure Hunt

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

Start Hunting!

Translated by