How to code dependent popup menus in Guide GUI

4 views (last 30 days)
Addison
Addison on 3 Sep 2013
Commented: Jan on 15 Jan 2018
I am trying to make a GUI that calculates certain values and characteristics for solenoids.
I want to have a popup menu that the user can choose between either single, heavy, or triple film enameled magnet wire.
I would like a second, dependent popup menu, to display the same list of gauge wire (15-30) with different variables associated to each choice. For example, the 20 gauge wire would have a different diameter in all three cases because of the enamels thickness.
How can I go about making the second popup menu dependent on the choice of popup menu 1?
Another way to describe it is to say:
Popup Menu 1: Choose A, B, or C
Popup Menu 2: Displays either [A1, A2], [B1, B2], or [C1, C2] as choices
I already have a working GUI that handles heavy film enameled wire that uses the following code:
function popupmenu1_Callback(hObject, eventdata, handles)
% hObject handle to popupmenu1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Determine wire gauge selection
str = get(hObject, 'String');
val = get(hObject, 'Value');
% Set current data to the selected data set
switch str{val}
case '20 ga - AWG Enameled Magnet Wire' % User selects 20 ga wire
handles.wg_input = .0173; % Wire radius
handles.tpi = 28.90; % Turns per inch
handles.rpi = .000844; % Resistance per inch
case '21 ga - AWG Enameled Magnet Wire' % User selects 21 ga wire
handles.wg_input = .0155; % Wire radius
handles.tpi = 32.26; % Turns per inch
handles.rpi = .001064; % Resistance per inch
.
.
.
case '30 ga - AWG Enameled Magnet Wire' % User selects 30 ga wire
handles.wg_input = .0058; % Wire radius
handles.tpi = 28.90; % Turns per inch
handles.rpi = .008643; % Resistance per inch
end
% Save the handles structure
guidata(hObject,handles)
I need to essentially make three lists like this corresponding to different wire radii, turns per inch, and resistance per inch that is dependent upon the first choice in popup 1.
This may be confusing, and I know I've repeated myself many times, so please let me know if you have any questions and I thank y'all in advance for any help.
  4 Comments
Elena Vollmer
Elena Vollmer on 15 Jan 2018
The GUI I'm creating has changed, so I don't actually need the exact same thing anymore. I contacted the Mathworks Support team to ask how to solve my new and this older problem and got a reply. In my case I want a popup menu to allow the user the choice between 3 models. Depending on what the user chooses the edit boxes further down should change. The reason being that my first two models require 5 user inputs while the third needs 7.
The question asked here is similar except that Addison wanted a second popup menu to display different choices depending on what choice the user had made in the first popup menu. Let's say the choices in popup menu 1 are "A", "B" and "C". Now if the user picks "A", popup menu 2 should allow only the choice between "I" and "II". If "B" is selected, the user can only choose either "III" or "IV", etc.
Below are the solutions suggested by the Mathworks Support Team to both Addison's and my problem.
1. Addison's Problem:
"Menu one is a listbox depending on the selected value in the listbox, adjust the content in the menu two, e.g."
function mwtest
a = uicontrol('style','listbox','String',{'A','B','C'},'Position',[20 20 100 200],'Callback',@myfun);
c = uicontrol('style','listbox','String',{'I','II'},'Position',[200 100 100 200]);
function myfun(~,~)
switch a.Value
case 1
c.String = {'I','II'};
case 2
c.String = {'III','IV'};
case 3
c.String = {'I','II','III','IV'};
end
end
end
2. My problem:
"Select the model, if it is model 1 or 2 display 5 text boxes if its model 3 display two additional text boxes. In your UI just always use 7 and toggle the visibility of 2 depending on the chosen model. I think the easiest is to use a listbox with the model options, e.g."
function mwtest
a = uicontrol('style','listbox','String',{'Model1','Model2','Model3'},'Position',[20 20 100 200],'Callback',@myfun);
uicontrol('style','edit','Position',[200 200 80 80]);
c = uicontrol('style','edit','Position',[200 300 80 80],'Visible','off');
function myfun(~,~)
if (a.Value == 3)
c.Visible = 'on';
else
c.Visible = 'off';
end
end
end
I hope this clarifies everything.
Jan
Jan on 15 Jan 2018
@Elena: Please do not hijack a foreign thread. You cannot accept an answer here, if it solves your problem. And if anybody posts an answer, it is not clear if it belongs to the original question or to your problem. So, please, open a new thread. Thanks.

Sign in to comment.

Answers (1)

Rik
Rik on 9 Jan 2018
Use code like this in the callback of the first popup menu:
popup2_database={{A1, A2},{B1, B2},{C1, C2}};
val=get(hObject,'Value');
set(handles.popup2,'String',popup2_database{val});
The third input of the set function needs to be a cell containing strings.

Categories

Find more on Migrate GUIDE Apps in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!