Best Practice for Testing for Appropriate Numerical Input

1 次查看(过去 30 天)
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
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
*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
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.

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

标签

产品

Community Treasure Hunt

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

Start Hunting!

Translated by