2017-03-06 86 views
0

我正在MATLAB中制作一个GUI,它将接受来自用户的数字输入并相应地进行计算。我希望能够在用户输入字母而不是数字时创建错误对话框。到目前为止,我有这段代码显示错误消息:Matlab gui错误信息

ed = errordlg('Please enter numbers only','Error'); set(ed, 'WindowStyle', 'modal');uiwait(ed); 

而这是主要的一段代码,我想的错误消息集成:

function roofspace_Callback(hObject, eventdata, handles) 
 
aSpace = str2double(get(hObject,'String')); %This is the user entered value for the roofspace. 
 
set(hObject,'UserData',aSpace); 
 

 
if aSpace==0 %If aSpace does not have anything then nothing is enabled. 
 
    set(findall(handles.uipanelFunds, '-property', 'enable'), 'enable', 'off'); 
 
    set(findall(handles.uipanelPanels, '-property', 'enable'), 'enable', 'off'); 
 
    set(findall(handles.uipanelUsage, '-property', 'enable'), 'enable', 'off'); 
 
    set(handles.calculate,'enable','off'); 
 
    set(hObject,'String',''); 
 
else %If aSpace hs a value then this enables the rest of the inputs. 
 
    set(findall(handles.uipanelFunds, '-property', 'enable'), 'enable', 'on'); 
 
    set(findall(handles.uipanelPanels, '-property', 'enable'), 'enable', 'on'); 
 
    set(findall(handles.uipanelUsage, '-property', 'enable'), 'enable', 'on'); 
 
    set(handles.calculate,'enable','on'); 
 
     
 
end

编辑: 总之,我需要弄清楚如何将我的错误消息的代码集成到这部分代码,以便它检查,如果用户已输入数字,否则我想显示一条错误消息。目前,无论用户输入什么内容,代码都会显示错误消息。

+1

有些洞察...什么?你的问题是什么? – excaza

+0

@excaza,当用户给出非数字输入时,如何获得此代码以显示错误消息?就目前而言,无论输入什么内容,都会显示错误消息。我不知道如何将错误消息代码集成到程序中以完成此操作 – Oreomega

+0

好像你更关注如何检查用户输入是否是一个数字,而不是在哪里嵌套if循环。 (如果它确实是你的代码,我相信你知道把if语句放在哪里..)。 – BillBokeey

回答

0

如下您可以检查一下:

我分裂aSpace = str2double(get(hObject,'String'));两个语句(只是因为它更容易解释):

str = get(hObject,'String'); 
aSpace = str2double(str); 

有两种错误情况下,我能想到的:

  1. 输入字符串不是数字。
    例如:str = 'abc'
    aSpace = 的NaN
    值也可能是Inf文件-Inf
  2. 该字符串是一个复数。
    例如:str = '2 + 3i'
    aSpace = 2.0000 + 3.0000i

使用以下if语句来检查,如果aSpaceNaN的天道酬勤-Inf,而不是复杂号:

is_ok = isfinite(aSpace) && isreal(aSpace); 

if (~is_ok) 
    %Handle error... 
end