动态地将字段添加到MATLAB GUI?

vic*_*tor 5 matlab user-interface

我正在使用GUIDE生成MATLAB GUI,但我想在用户点击按钮时创建字段.有没有办法在回调中动态添加新的GUI对象?

gno*_*ice 6

实现此目的的一种方法是在开始时创建GUI对象,但将其"可见性"属性设置为"关闭".然后,当用户单击按钮时,将"可见性"属性设置为"打开".这样,当GUI运行时,您将不会创建新的GUI对象,您只需更改它的哪些部分是可见的.

编辑:如果你不知道在运行时需要多少新的GUI对象,那么就是如何将新的GUI对象添加到句柄结构中(其中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
Run Code Online (Sandbox Code Playgroud)

那么你当然必须为新的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
Run Code Online (Sandbox Code Playgroud)

我在上面的代码中使用的感兴趣的函数是:GUIDATA(用于存储/检索GUI的数据)和GCBF(获取其回调当前正在执行的对象的父图的句柄).