MATLAB: How to populate the GUI dynamically in MATLAB

"hiddenguihideMATLABobjectsstartvisible

I have objects in my GUI that I only want to be visible if a certain selection is made or action is performed. I would like to know how to achieve this.

Best Answer

Here are two basic ways to dynamically make the objects in your GUI visible:
-------------------------------------------
1. Create the objects dynamically. Store the handles for the dynamic objects in the 'handles' structure; when the popupmenu callback is called, you could then delete the existing objects and create new ones. Here is a basic start for the code, where the visible objects depend on the selected value in a popup menu.
% determine which item in the popup menu was selected

v = get(handles.popupmenu);
% delete the objects that already exist. in this case, the handles.button and
% handles.axes field are always used to store the handles for the currently
% existing objects
delete(handles.button)
delete(handles.axes);
% create new objects, based on the selection
switch v
case 1
% create Button 1
handles.button = uicontrol('style','pushbutton',...
% create Axes 1
handles.axes = axes(...
case 2
% create Button 2
handles.button = uicontrol('style','pushbutton',...
% create Axes 2
handles.axes = axes(...
end
% store the 'handles' structure that contains the new handle values
guidata(handles.figure(handles)
Make sure the proper fields already exist in the 'handles' structure before you enter the popup menu's callback. Otherwise, you may receive an error if you try to refer to a field that does not exist. If you do not want the objects to exist when the object starts, initialize the fields to empty arrays in the GUI's OpeningFcn. If you want one of the sets of objects to exist, try one of the following:
a. Lay out the GUI to start with a button and axes with the Tag values 'button' and 'axes' respectively, so you can use the same fields in the 'handles' structure
b. Create a button and axes in the GUI's OpeningFcn, and store their handles in the 'handles' structure.
-------------------------------------------
2. First, create all of the objects while you are laying out the GUI figure. Then, use their 'Visible' property to select whether they appear. Using the same example as above, in which the objects depend upon a selection in a popup menu, you could use something like this:
% determine which item in the popup menu was selected
v = get(handles.popupmenu);
% set the Visible property of the objects based on the selection
switch v
case 1
set(handles.button1,'Visible','on');
set(handles.axes1,'Visible','on');
set(handles.button2,'Visible','off');
set(handles.axes2,'Visible','off');
case 2
set(handles.button1,'Visible','off');
set(handles.axes1,'Visible','off');
set(handles.button2,'Visible','on');
set(handles.axes2,'Visible','on');
end
In this case, all of the objects exist in the GUI at the same time. However, only one group is visible at any time, based on the selection in the popup menu. In this case, you need to remember to set the 'Visible' property for one of the groups of objects to 'off' in GUIDE, so only one group is visible when the GUI starts.