我正在使用GUIDE生成MATLAB GUI,但我希望在用户单击按钮时创建字段。是否有办法在回调函数中动态添加新的GUI对象?
我正在使用GUIDE生成MATLAB GUI,但我希望在用户单击按钮时创建字段。是否有办法在回调函数中动态添加新的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(获取当前执行回调函数对象的父图形句柄)。