0

working on a project and have to make sure the input in a textbox meets a requirement for both being numeric, but also not being equal to zero. So far this is my code:

function minValue_Callback(hObject, eventdata, handles)
% hObject    handle to edit10 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit10 as text
%        str2double(get(hObject,'String')) returns contents of edit10 as a     double
user_entry_X = str2double(get(hObject,'string'));
if isnan(user_entry_X)
 errordlg('You must enter a numeric value','Error!','modal')
 uicontrol(hObject)
return
end

Another question is, I have two textboxes, minValue and maxValue. How can I also make sure that the data in maxValue > minValue? (Values are used in a for loop, and I thought checking before hand and showing an error would be better.)

3
  • 1
    What is your question? Where is the problem implementing that? Commented Dec 6, 2015 at 22:41
  • @Daniel Question is how to make sure input is not zero but is also numeric. And is it possible to call this CallBack from a different function or on a button press? Commented Dec 6, 2015 at 23:09
  • @Link For the second part, in maxValueText_callback and minValueText_callback, why not check against limits, and adjust the limits of the other according to the current value that has just changed? Commented Dec 21, 2015 at 15:17

2 Answers 2

1

So, if I understand correctly, you should change the lines:

if isnan(user_entry_X)
    errordlg('You must enter a numeric value','Error!','modal');

to be:

if isnan(user_entry_X) || user_entry_X == 0
    errordlg('You must enter a non-zero numeric value','Error!','modal');

For the second part of the question, I don't understand the difficulty. Just type:

if maxValue > minValue
    ...
end
Sign up to request clarification or add additional context in comments.

6 Comments

For 2nd part I need to check when they're entered in either textbox, unsure how to do that.
isnan is required because that's what str2double returns for invalid inputs. isnumeric returns true for NaN and str2double only returns scalar numeric values (for string inputs) by definition.
@horchler Do you have an idea of what to do then?
Fixed. Thanks @horchler. FYI, this solution accepts complex numbers. If you don't want to accept complex input, change the test to if isnan(user_entry_X) || ~isreal(user_entry_X) || user_entry_X == 0
Do you also want to check for >= 0, or is negative input valid?
|
0

You probably want to use

errordlg('You must enter a numeric value','Error!','modal')
h = uicontrol(hObject)
uiwait(h)

So the error dialogue stays open until the user acknowledges. (I fell into the trap beleiving, possibly from windows programming, that 'modal' creates this behaviour.)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.