查看单个帖子
旧 2019-12-10, 16:49   #1
poster
高级会员
 
注册日期: 2019-11-21
帖子: 3,006
声望力: 66
poster 正向着好的方向发展
帖子 是否将字段动态添加到MATLAB GUI?

我正在使用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 (获取当前正在执行回调的对象的父图形的句柄)。



更多&回答...
poster 当前离线   回复时引用此帖