1
votes

I have a GUI called GUI_main in which I have a pushbutton called pushbutton_GUI_main. I currently have the following callback function implemented:

function pushbutton_GUI_main_Callback(hObject, eventdata, handles)

GUI_sub

Where GUI_sub is another GUI that opens when you click on pushbutton_GUI_main. However, I want something like the following:

function pushbutton_GUI_main_Callback(hObject, eventdata, handles)

if (GUI_sub == open)
   close(GUI_sub)
else
   GUI_sub

That is, with pushbutton_GUI_main I want to be able to open and close GUI_sub.

2
Does GUI_sub return a handle to itself? If so, you can probably store that handle in your GUI_main handles and then check its status. I haven't done that exact thing so I can't help with the exact implementationTrogdor

2 Answers

2
votes

You need an object handle to reference the sub GUI. Assuming GUI_sub is a GUI built with GUIDE, it is programmed by default with an optional handle output.

A naive implementation for a GUIDE GUI would look something like this:

function pushbutton1_Callback(hObject, eventdata, handles)
if ~isempty(handles.figure1.UserData)
    close(handles.figure1.UserData);
    handles.figure1.UserData = [];
else
    handles.figure1.UserData = sub_GUI;
end

Most of (maybe all?) MATLAB's graphics objects have a UserData field by default. I've utilized the UserData of the base figure object for this simple example. See also: Share Data Among Callbacks for other methods to store/transfer this data.

1
votes

As excaza says, handles is a great way to pass data or information in a GUI. Another way, if you for some reason don't want to store the GUI handle, perhaps if the GUI_sub could be created independently is to search for the figure handle.

subGuiH = findall(0,'Name','GUI_sub');
if ~isempty(subGuiH)
    close(subGuiH);
end
GUI_sub;

The search could be narrowed by adding

findall(0,'Type','figure','Name','GUI_sub')

Depending on your Matlab version, you could also checkout groot