matlab: close the gui if the user presses the close button

16,002

Solution 1

I succeeded!

I used setappdata and getappdata in order to know if the second callback was called.

@Derek, Derek, thank you very much for your help and that you spent a lot of time for me!

function  data = mainGUI(options, files)
 %# current options
 j = 1;
 ops = cellfun(@(c) c(1), options, 'Uniform',false);
 data{j} =  [ops{1:length(ops)}];
 j = j + 1;


 options = cellfun(@(c) c(2:1:end), options, 'Uniform',false);
 clear ops;
 ops = cellfun(@(c) c(1), options, 'Uniform',false);
 opts =  [ops{1:length(ops)}];

%# create main figure, with plot and options button
hFig = figure('Name','window 1','Visible','Off');
callback


%# options button callback function
function callback(o,e)
    %# save current options (sharing data between the two GUIs)
    setappdata(hFig, 'opts',opts);

    %# display options dialog and wait for it

    for k=1: length(files)
            hOptsGUI = secondaryGUI(hFig, options, k, length(files));

            img = imread(files{k});  %# Read the data from your image file
            hAxes = axes('Parent',hOptsGUI,'Units','pixels','Position',[362 242 424 359]);  %#   so the position is easy to define
            image(img,'Parent',hAxes);  %# Plot the image
            set(hAxes,'Visible','off');          %# Turn the axes visibility off

            out = 'FALSE';
            setappdata(hFig,'some_var',out);
            % show the images
            %%Im = imread(files{k});
            %%AxesH = axes('Units', 'pixels', 'position', [0.5, 10, 400, 260], 'Visible', 'off');
            %%image('Parent', AxesH, 'CData', Im); %# add other property-value pairs as needed

            waitfor(hOptsGUI);

            some_other_var = getappdata(hFig,'some_var');

            if (strcmp(some_other_var, 'OK') == 1)
                 %# get new options, and update plot accordingly
                opts = getappdata(hFig, 'opts');
                data{j} = opts;
                j = j + 1;
            else
                k = length(files);
                data = 0;
                return;
            end;
    end
end
end

function hFig = secondaryGUI(hParentFig, options, k, num_files)
%# create figure


hFig = figure('Name','Step 3 of 4: Choose data for each image','Menubar','none', 'Resize','off', ...
    'WindowStyle','modal', 'Position',[300 300 1150 600]);
set(gcf,'NumberTitle','off')
movegui(hFig, 'center');



options = cellfun(@(c) c(end:-1:1), options, 'Uniform',false);
num = length(options);

%# get saved settings
selected = getappdata(hParentFig, 'opts');

%# top/bottom panels
hPanBot = uipanel('Parent',hFig, 'BorderType','none', ...
    'Units','normalized', 'Position',[0 0.0 1 0.2]);

hPanTop = uipanel('Parent',hFig, 'BorderType','none', ...
    'Units','normalized', 'Position',[0 0.2 1 0.2]);



%# buttongroups in top panel
hBtnGrp = zeros(1,num);
width = 1/num;
for i=1:num
    %# create button group
    hBtnGrp(i) = uibuttongroup('Parent',hPanTop, ...
        'Units','normalized', 'Position',[(i-1)*width 0 width 1]);
    %# populate it with radio buttons
    height = 1./numel(options{i});
    for j=1:numel(options{i})
        h = uicontrol('Parent',hBtnGrp(i), 'Style','Radio', ...
            'Units','normalized', 'Position',[0.05 (j-1)*height 0.9 height], ...
            'String',options{i}{j});
        %# set initially selected values
        if strcmp(selected{i},options{i}{j})
            set(hBtnGrp(i), 'SelectedObject',h)
        end
    end
end

if k ~= num_files
%# save button in bottom panel
uicontrol('Parent',hPanBot, 'Style','pushbutton', ...
    'Units','normalized', 'Position',[0.3 0.2 0.4 0.2], ...
    'String','next', 'Callback',@callback);
else
    uicontrol('Parent',hPanBot, 'Style','pushbutton', ...
    'Units','normalized', 'Position',[0.3 0.2 0.4 0.2], ...
    'String','start', 'Callback',@callback);
end
%# save button callback function
function callback(o,e)
    out = 'OK';
    setappdata(hParentFig,'some_var',out);
    %# get selected values
    hObjs = get(hBtnGrp(:), 'SelectedObject');
    vals = get(cell2mat(hObjs),{'String'});

    %# update settings
    setappdata(hParentFig, 'opts',vals);

    %# close options dialog
    close(hFig)
end

function cmdClose_Callback(hObject,varargin)  
    %do cleanup here
    delete(hFig);
end
end

Solution 2

Just check to see if hFig1 is a valid handle before you create the second figure.

if ishandle(hFig1)
    hFig2 = figure(...)
else
    return;      % do something else
end

Repeat as needed.


function data = mainGUI(options, files)
    % ...
    % create main figure, with plot and options button
    hFig = figure('Name','window 1','Visible','Off');

    complete = callback;
    if complete == 1
        data = data;  
    else 
        data = 0;
    end

% options button callback function
function complete = callback(o,e)
    %save current options (sharing data between the two GUIs)
    setappdata(hFig, 'opts',opts);

    % display options dialog and wait for it
    complete = 0;
    for k = 1 : length(files)
        hOptsGUI = secondaryGUI(hFig, options, k, length(files));
        % ...
    end
    % we reached the end of the loop!
    complete = 1;
end
Share:
16,002
Alon Shmiel
Author by

Alon Shmiel

Updated on June 04, 2022

Comments

  • Alon Shmiel
    Alon Shmiel almost 2 years

    I made a GUI that gets 2 elements: files (list of files) and options (I will explain later).

    there is a function that calls the function below:

    // asume that this is my 'options'
    options{1} = {'Treatment', 't1', 't2'};
    options{2} = {'Tree', '1', '2'};
    options{3} = {'Replica', 'r1', 'r2', 'r3'};
    
    // asume that this is my 'files' (may be more than 2 images
    files{1} = 'C:\Documents and Settings\erezalon\Desktop\gib_proj_02.10.12\fina3_32bit\IMG_4640.JPG';
    files{2} = 'C:\Documents and Settings\erezalon\Desktop\gib_proj_02.10.12\fina3_32bit\grapes03.jpg';
    
    
    mydata = mainGUI(options, files).
    
    // here I want to check 'mydata'. if it is equal to zero or not.
    

    for each image, the GUI is created in the function 'mainGUI' (but each time, one GUI is showed till the user presses 'next').

    I want to do the next thing:

    if the user presses the close button in the GUI, I want to stop the displaying of the other GUI-s and set 'data = 0'. then, I will check what is the returned element ('mydata') and if it's equal to 0, I will know that the user closed the GUI and I will act as needed.

    I tried to do this by the 'cmdClose_Callback' but it doesn't work.

    assume that 'files' = two images, so this is the GUI of the first image.

    I press the close button:

    enter image description here

    and got the GUI of the second image despite of closing the GUI of the first image.

    enter image description here

    I want that when I close the GUI, the other GUI-s don't appear.

    this is my code:

    function  data = mainGUI(options, files)
     %# current options
     j = 1;
     ops = cellfun(@(c) c(1), options, 'Uniform',false);
     data{j} =  [ops{1:length(ops)}];
     j = j + 1;
    
    
     options = cellfun(@(c) c(2:1:end), options, 'Uniform',false);
     clear ops;
     ops = cellfun(@(c) c(1), options, 'Uniform',false);
     opts =  [ops{1:length(ops)}];
    
    %# create main figure, with plot and options button
    hFig = figure('Name','window 1','Visible','Off');
    a = 1
    callback
    
    %# options button callback function
    function callback(o,e)
        a = 2
        %# save current options (sharing data between the two GUIs)
        setappdata(hFig, 'opts',opts);
    
        %# display options dialog and wait for it
    
        for k=1: length(files)
    
                hOptsGUI = secondaryGUI(hFig, options, k, length(files));
    
                img = imread(files{k});  %# Read the data from image file data
                hAxes = axes('Parent',hOptsGUI,'Units','pixels','Position',[362 242 424 359]);  %#   so the position is easy to define
                image(img,'Parent',hAxes);  %# Plot the image
                set(hAxes,'Visible','off');          %# Turn the axes visibility off
    
                a = 3
    
                waitfor(hOptsGUI);
    
                a = 4
                %# get new options, and update plot accordingly
                opts = getappdata(hFig, 'opts');
                 data{j} = opts;
                 j = j + 1;
        end
    end
    
    end
    
    function hFig = secondaryGUI(hParentFig, options, k, num_files)
    %# create figure
    
    a = 5
    
    
    hFig = figure('Name','Step 3 of 4: Choose data for each image','Menubar','none', 'Resize','off', ...
        'WindowStyle','modal', 'Position',[300 300 1150 600], 'CloseRequestFcn',@cmdClose_Callback);
    set(gcf,'NumberTitle','off')
    movegui(hFig, 'center');
    
    
    
    options = cellfun(@(c) c(end:-1:1), options, 'Uniform',false);
    num = length(options);
    
    %# get saved settings
    selected = getappdata(hParentFig, 'opts');
    a = 6
    %# top/bottom panels
    hPanBot = uipanel('Parent',hFig, 'BorderType','none', ...
        'Units','normalized', 'Position',[0 0.0 1 0.2]);
    
    hPanTop = uipanel('Parent',hFig, 'BorderType','none', ...
        'Units','normalized', 'Position',[0 0.2 1 0.2]);
    
    
    
    %# buttongroups in top panel
    hBtnGrp = zeros(1,num);
    width = 1/num;
    for i=1:num
        %# create button group
        hBtnGrp(i) = uibuttongroup('Parent',hPanTop, ...
            'Units','normalized', 'Position',[(i-1)*width 0 width 1]);
        %# populate it with radio buttons
        height = 1./numel(options{i});
        for j=1:numel(options{i})
            h = uicontrol('Parent',hBtnGrp(i), 'Style','Radio', ...
                'Units','normalized', 'Position',[0.05 (j-1)*height 0.9 height], ...
                'String',options{i}{j});
            %# set initially selected values
            if strcmp(selected{i},options{i}{j})
                set(hBtnGrp(i), 'SelectedObject',h)
            end
        end
    end
    
    if k ~= num_files
    %# save button in bottom panel
    uicontrol('Parent',hPanBot, 'Style','pushbutton', ...
        'Units','normalized', 'Position',[0.3 0.2 0.4 0.2], ...
        'String','next', 'Callback',@callback);
    
    else
        uicontrol('Parent',hPanBot, 'Style','pushbutton', ...
        'Units','normalized', 'Position',[0.3 0.2 0.4 0.2], ...
        'String','start', 'Callback',@callback);
    end
    
    
    %# save button callback function
    function callback(o,e)
        a = 7
        %# get selected values
        hObjs = get(hBtnGrp(:), 'SelectedObject');
        vals = get(cell2mat(hObjs),{'String'});
    
        %# update settings
        setappdata(hParentFig, 'opts',vals);
    
    
        close(hFig)
        a = 8
    end
    
     function cmdClose_Callback(hObject,varargin)
        disp(['Close Request coming from: ',get(hObject,'Type')]);
        a = 9
    
        %do cleanup here
        delete(hFig);
        a = 10
    
     end
    end
    

    if 'files' has two images, I push the 'next' button for the first figure, and in the second figure I close, I get:

    a = 1
    a = 2
    a = 5
    a = 6
    a = 3
    a = 7
    Close Request coming from: figure
    a = 9
    a = 10
    a = 8
    a = 4
    a = 5
    a = 6
    a = 3
    Close Request coming from: figure
    a = 9
    a = 10
    a = 4
    

    above the line: hOptsGUI = secondaryGUI(hFig, options, k, length(files)); I tried to put some lines. In order to testthe lines, I print a fit message:

    if (ishandle(hFig))
         disp('exists');
    else disp('was closed');
    end;
    

    but it doesn't work :/

    for each GUI that the user will close, the next callback will called:

    function callback(o,e)
    
        %# get selected values
        hObjs = get(hBtnGrp(:), 'SelectedObject');
        vals = get(cell2mat(hObjs),{'String'});
    
        %# update settings
        setappdata(hParentFig, 'opts',vals);
    
        %# close options dialog
        close(hFig)
    
    end
    

    so I just need to know in the next 'for loop' if this callback was called:

    for k=1: length(files)
    

    how can I do that?

  • Alon Shmiel
    Alon Shmiel over 11 years
    are you sure? I think it's not be a valid handle anyway, because the handle can be closed by the 'close' button or when the user presses the 'next' button.
  • Derek
    Derek over 11 years
    @AlonShmiel Not sure if I understand what you are trying to do. See updated code.
  • Derek
    Derek over 11 years
    @AlonShmiel If figure 1 is closed by the user, then we don't create figure 2. Is this what you want?
  • Alon Shmiel
    Alon Shmiel over 11 years
    yes, I am trying what you suggested and I will let you no if it works or doesn't. thank you!
  • Alon Shmiel
    Alon Shmiel over 11 years
    I think I don't know where to check it.. I checked it in the 'for loop' of 'callback', below the line: hOptsGUI = secondaryGUI(hFig, options, k, length(files));
  • Derek
    Derek over 11 years
    I think you probably want to check it before that line, i.e. don't call that line if the main figure is closed.
  • Derek
    Derek over 11 years
    @AlonShmiel OK, how does the function callback know about the first figure handle, hFig? That is the handle of the first window, right? I don't see how hFig is defined in the second function.
  • Derek
    Derek over 11 years
    @AlonShmiel Are you creating a new figure for the second step or are you reusing the existing figure? If a figure is closed, it's handle will become invalid. There is no getting around that.
  • Alon Shmiel
    Alon Shmiel over 11 years
    I think you are right. I have two figures. the first figure is invisible, and the second figure is reused again and again (if you have a time, see my updating topic). thank you very much!!! :]
  • Derek
    Derek over 11 years
    @AlonShmiel And if any of the "second figures" are closed, you don't want to make the first figure visible?
  • Alon Shmiel
    Alon Shmiel over 11 years
    no, I want that if the "second figures" are closed, all the figures will be closed and the returned element ('data') of this function will be equal to 0. thank you for your help!
  • Derek
    Derek over 11 years
    @AlonShmiel What if you set a flag after the for loop in the callback function? So, if we reach the end of the for loop, we can assume that the user completed all of the tasks. Thus, we set a flag and return this flag to the mainGUI function. The mainGUI function will then return the data or 0. Make some sense?
  • Alon Shmiel
    Alon Shmiel over 11 years
    I am updating my topic again. I put a=1, a=2, ... a=10 in the code in order to know whats happens.
  • Derek
    Derek over 11 years
    @AlonShmiel Have a look at my updated answer. Would this work?
  • Alon Shmiel
    Alon Shmiel over 11 years
    it doesn't work.. can you please copy my code to your matlab and run it? I will explain again..
  • Alon Shmiel
    Alon Shmiel over 11 years
    I am going to sleep. the time here is 6:45 in the morning and I didn't sleep all the night because I tried to fix this problem. thank you again and again! Alon.
  • Alon Shmiel
    Alon Shmiel over 11 years
    I succeed something: I have a parameter. if it's equal to "FALSE" I have to close all the figures. now I am trying to close all. I will update you.. thank you very much for everything!