我正在使用GUIDE生成MATLAB GUI,但是我想在用户单击按钮时创建字段。有没有办法在回调中动态添加新的GUI对象?
回答:
实现此目的的一种方法是在开始时创建GUI对象,但是将其“ Visibility”属性设置为“ off”。然后,当用户单击按钮时,将“可见性”属性设置回“开”。这样,您将不会在GUI运行时创建新的GUI对象,而只需更改它的哪些部分可见或不可见。
编辑:如果在运行时之前不知道需要多少个新的GUI对象,这就是将新的GUI对象添加到handles结构中的方式(其中
hFigure是GUI图形的句柄):
p = uicontrol(hFigure,'Style','pushbutton','String','test',... 'Callback',@p_Callback); % Including callback, if needed handles.test = p; % Add p to the "test" field of the handles structure guidata(hFigure,handles); % Add the new handles structure to the figure 然后,您当然必须为新的GUI对象编写回调函数(如果需要),它可能看起来像这样:
function p_Callback(hObject,eventdata) handles = guidata(gcbf); % This gets the handles structure from the figure ... (make whatever computations/changes to GUI are needed) ... guidata(gcbf,handles); % This is needed if the handles structure is modified 我在上面的代码中使用的感兴趣的函数是:
GUIDATA (用于存储/检索GUI的数据)和
GCBF (获取当前正在执行回调的对象的父图形的句柄)。
更多&回答...