0

I'm making a GUI in MATLAB that will accept numerical inputs from the user and make calculatations with them accordingly. I want to be able to create an error dialog box whenever the user enters letters instead of numbers. So far I have this bit of code to display the error message:

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

And this is a section of the main code that I would like to integrate the error message in:

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

EDIT: In summary, I need to figure out how I can integrate my error message code into this section of code so that it checks if the user has inputted numbers, otherwise I would like an error message to display. At the moment, the code displays the error message regardless of what the user has inputted.

il_raffa
  • 5,090
  • 129
  • 31
  • 36
Oreomega
  • 1
  • 1
  • 1
    Some insight into...what? What is your question? – sco1 Mar 06 '17 at 15:28
  • @excaza, how do I get this code to display an error message when a non-numerical input is given by the user? As it stands now, the error message displays regardless of what is inputted. I don't know how to integrate the error message code into the program to accomplish this – Oreomega Mar 06 '17 at 15:34
  • Seems like you're more looking after how to check if an user input is a number than after where to nest the if loop. (If it indeed is your code I'm sure you know where to put the if statement..). – BillBokeey Mar 06 '17 at 15:46
  • @BillBokeey, you're right and I edited the question to be more clear. – Oreomega Mar 06 '17 at 16:06

1 Answers1

0

You can check it as follows:

I split aSpace = str2double(get(hObject,'String')); to two statements (just because it's easier to explain):

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

There are two error cases I can think of:

  1. The input string is not numeric.
    Example: str = 'abc'.
    aSpace = NaN
    Value might also be Inf or -Inf.
  2. The string is a complex number.
    Example: str = '2 + 3i'.
    aSpace = 2.0000 + 3.0000i

Use the following if statement to check if aSpace is not NaN, Inf, -Inf and not complex number:

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

if (~is_ok)
    %Handle error...
end
Rotem
  • 30,366
  • 4
  • 32
  • 65