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
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.
采纳的回答
dpb
2015-6-5
*isfinite* removes the NaN and Inf possibilities
*isreal* tests whether is a complex nonzero imaginary part
更多回答(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.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!