tags:

views:

977

answers:

2

I'm generating a MATLAB GUI using GUIDE, but I want to create fields when a user clicks on a button. Is there any way to dynamically add new GUI objects in the callbacks?

+3  A: 

Using UICONTROL, you will be able to add 'fields' (called uicontrols or widgets).

You will want to specify the style to get edit boxes, buttons, etc...

You may actually want to have all the widgets already there in GUIDE and then just change the visibility or enabled property as needed.

You can find my video tutorials on GUI building in MATLAB here: http://blogs.mathworks.com/videos/category/gui-or-guide/

This should cover this and many related topics in GUI building.

MatlabDoug
Can I add the created uicontrol object to the handles that are generated? Something like set(handles.test, uicontrol_element)
victor
when you make the UICONTROL, capture the handle:handles.newWidget = uicontrol(.......)
MatlabDoug
+3  A: 

One way to accomplish this is to create the GUI objects at the start, but set their "Visibility" property to "off". Then, when the user clicks on a button, you set the "Visibility" property back to "on". This way, you won't be making new GUI objects while the GUI is running, you would simply be changing which parts of it are visible or not.

EDIT: If you don't know how many new GUI objects you need until run-time, this is how you would add the new GUI objects to the handles structure (where hFigure is a handle to the GUI figure):

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

You would then of course have to write the callback function for the new GUI object (if it needs one), which might look something like this:

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

Functions of interest that I used in the above code are: GUIDATA (for storing/retrieving data for a GUI) and GCBF (get handle of parent figure of the object whose callback is currently executing).

gnovice
The problem is I don't know how many I'll have to create until runtime. Can I do it by altering the gui handles created? I'm trying to do: p = uicontrols(figure, 'Style','pushbutton','String','test') set(handles.test = p)but it's giving me errors. Is there another way?
victor
the set command listed above is a problem. What are you trying to accomplish with it?
MatlabDoug
Alright, I'm trying gnovice's edit, and for some reason when I run that in a callback generated by guide, it ends up changing an existing element. My code: new_t = uicontrol(hObject,'Style','edit')then I try:K>> get(new_t, 'Style')ans =pushbuttonIt's strange because set(handles.test, 'Style','edit') ends up changing handles.pushbutton.Style.Any ideas?
victor
Is hObject a handle to the figure, or another GUI object? I have a feeling hObject is a handle to a pushbutton. Try using the GCBF function instead: new_t = uicontrol(gcbf,'Style','edit');
gnovice
Thank you, that was it!
victor