Rivasa
Rivasa

Reputation: 6750

Matlab check if input is nonezero

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.)

Upvotes: 0

Views: 96

Answers (2)

Nigel Davies
Nigel Davies

Reputation: 1690

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.)

Upvotes: 0

houtanb
houtanb

Reputation: 4100

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

Upvotes: 1

Related Questions