Matlab检查输入是否为零

时间:2015-12-06 22:12:27

标签: matlab matlab-guide

处理项目并且必须确保文本框中的输入满足数字的要求,但也不等于零。到目前为止,这是我的代码:

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

另一个问题是,我有两个文本框,minValue和maxValue。我怎样才能确保maxValue > minValue中的数据? (值在for循环中使用,我认为事先检查并显示错误会更好。)

2 个答案:

答案 0 :(得分:1)

所以,如果我理解正确,你应该改变这些行:

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

是:

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

对于问题的第二部分,我不明白这个难点。只需输入:

if maxValue > minValue
    ...
end

答案 1 :(得分:0)

您可能想要使用

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

因此,在用户确认之前,错误对话将保持打开状态。 (我陷入了陷阱,可能来自Windows编程,“模态”会产生这种行为。)