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
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
2015년 6월 5일
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
2015년 6월 5일
채택된 답변
추가 답변 (1개)
Sean de Wolski
2015년 6월 5일
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 Exchange에서 Characters and Strings에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!