-1

I am trying to make it so the application/gui closes completely if the user clicks cancel or exit of an input dialog. The tag of my gui is called window however using close(handles.window); leads to the program looping through once and then (after the user clicks cancel or exit again) reaching the close(handles.window); line which, of course, leads to an Invalid figure handle error.

Is there any method to close the application without the need for producing an error and not closing the entire MATLAB environment (like quit and exit). The error() is my temporary fix which works but I don't want the application to seem like it has crashed.

I have tried close all but that has no effect. It's worth noting that the application does reach inside the if statement if the user clicks cancel or exit.

I have also tried setting a variable and having the close commands outside the while loop.

apa = getAvailableComPort();
apList = '';

i = 0;
for idx = 1:numel(apa)
    if i == 0
        apList = apa(idx);
    else
        apList = strcat(apList, ', ', apa(idx));
    end
    i = i + 1;
end

prompt = {'Enter COM PORT:'};
title = 'COM PORT';
num_lines = 1;
def = apList;
COM_PORT = inputdlg(prompt,title,num_lines,def); 
% Keep asking for a COM PORT number until user gives a valid one
while sum(ismember(apa, COM_PORT)) == 0 % If the COM port comes up in the available COM ports at least once
    % If user clicks cancel, close the guide
    if isempty(COM_PORT) == 1
        error('Closing...'); % HERE IS THE PROBLEM
    end
    prompt = {'Invalid COM PORT'};
    title = 'Invalid COM PORT';
    COM_PORT = inputdlg(prompt,title,num_lines,def);    
end

The function getAvailableComPort is found at http://www.mathworks.com/matlabcentral/fileexchange/9251-get-available-com-port

This entire piece of code is at the top of the gui_OpeningFcn() function.

2
  • I have updated my question with the relevant code, sorry for not doing this initially. It isn't the whole thing but that should be enough.
    – Roy
    Jul 14, 2015 at 15:45
  • How do you start that gui? Where handles.window gets stored?
    – Matt
    Jul 14, 2015 at 16:25

4 Answers 4

1

Have you tried putting a break after the close(handles.window). The break will exit the while loop so it won't hit that line again.

if isempty(COM_PORT) == 1
    close(handles.window)
    break
end

This way the while loop stops after closing the window. IF you need to do more cleanup besides just closing the window then set an error flag .

%Above while loop
errorFlag = False;

if isempty(COM_PORT) == 1
    close(handles.window)
    errorFlag = True;
    break
end

%OutSide of While loop
if errorFlag
    %Do some more clean-up / return / display an error or warning
end

Also FYI, you don't need to do isempty(COM_PORT) == 1 ... isempty(COM_PORT) will return true/false without the == 1

2
  • The isempty(..)==1 was just for me to be 100% sure of the issue I was highlighting. Unfortunately, adding the break just exits the while loop and then the program crashes later on when It tries to do set(handles..)
    – Roy
    Jul 14, 2015 at 15:48
  • Without more context of where this piece of code is in your overall GUI/M File it is hard to tell you how to fix it. However most likely you should look at my second example and put a "return" statement in it to exit from whatever function this is code fragment is in.
    – Aero Engy
    Jul 14, 2015 at 16:10
1

Your requirements aren't really clear, but can't you just protect the close operation by doing

if ishandle(handle.window)
   close(handle.window)
end

This will prevent the close from being attempted if the window has already been destroyed.

1
  • Well the program will reach inside that if statement once, for the first time, but then loop back around and carry on (assuming the user keeps pressing cancel). And, so the program never actually closes.
    – Roy
    Jul 15, 2015 at 8:20
0

Why don't you use a simple return and a msgbox to inform that user has clicked on Cancel?

if isempty(COM_PORT)
    uiwait(msgbox('Process has been canceled by user.', 'Closing...', 'modal'));
    delete(your_figure_handle_here);
    return;
end
-1

I tried using the return statement in conjunction with close(handles.window) but received the error:

Attempt to reference field of non-structure array.

Error in ==> gui>gui_OutputFcn at 292 varargout{1} = handles.output;

So it seems just removing that line on 292 solved the issue.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.