How to make a GUI pushbutton open/close another GUI

时间:2016-08-31 17:01:04

标签: matlab user-interface matlab-guide

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 个答案:

答案 0 :(得分:2)

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 :(得分:1)

正如 excaza 所说,句柄是在GUI中传递数据或信息的好方法。 另一种方式,如果由于某种原因你不想存储GUI句柄,也许如果可以独立创建GUI_sub就是搜索图形句柄。

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

可以通过添加

来缩小搜索范围
findall(0,'Type','figure','Name','GUI_sub')

根据您的Matlab版本,您还可以结帐 groot