Creating Uicontrol drop down menus with varied selections

How can you create a uicontrol drop down menu's that are dependent on the selection of the previous menu. Basically I want to be able to have a list of brands then select one and have a list of models and from the brand selection it will only show the models associated to that brand. I am thinking using if statements and a callback but am not sure how to put it all together. Any help would be great thank you.

1 Comment

If the user selects a item from the first popupmenu only I want only certain items of the string of the second popupmenu to show.

Sign in to comment.

 Accepted Answer

Are you doing this programmatically, in App Designer or GUIDE?
You can't hide items in a dropdown menu list. You can hide it until a selection is made in another dropdown list and populate its items based on what was selected.
You would use a callback function that will execute when the value is changed.
If I assume App Designer:
% Value changed function: DropDown
function DropDownValueChanged(app, event)
value = app.DropDown.Value;
app.DropDown2.Visible = 1;
switch value
case 'Option 1'
app.DropDown2.Items = {'A','B','C'};
case 'Option 2'
app.DropDown2.Items = {'D','E','F'};
case 'Option 3'
app.DropDown2.Items = {'G','H','I'};
end
end

11 Comments

I am actually using GUIDE, this is what I have right now:
I am trying to have the b string valuese depend on the selection of the a string and so on
f = figure('Name','Engine Volumeteric Efficency and Parameter Determinant','NumberTitle','off');
a = uicontrol(f,'Style','popupmenu');
a.Position = [25 350 90 20];
a.String = {'Select Brand','Toyota','Chevy','Honda','Ford','Dodge','Jeep'};
b = uicontrol(f,'Style','popupmenu');
b.Position = [25 325 90 20];
b.String = {'Select Model','Tacoma','Silverado','Tundra','Colorado',};
c = uicontrol(f,'Style','popupmenu');
c.Position = [25 300 90 20];
c.String = {'Select Engine','V6 4.0'};
It looks like you are actually doing it programmatically. It might be slightly easier in GUIDE, but that's your choice. There are some helpful examples you should look at here.
The solution would be very similar. Capture the value of 'a'. Use a switch statement to populate the items of 'b'. The challenge is going to be allowing one uicontrol to access the properties of another uicontrol. Because each callback is a function, it only has access to what is passed in. This is where GUIDE is helpful - it passes in a handle to all the gui objects. Still, there is a way to do this programmatically which is not too difficult.
To spare you having to figure this all out on your own, here is an updated though incomplete example. It is enough for you to expand it for your full example.
function popupGUI
figureHandle = figure('Name','Engine Volumeteric Efficency and Parameter Determinant','NumberTitle','off');
handles.make = uicontrol('Style','popupmenu','Callback',@popup_menu_Callback);
handles.make.Position = [25 350 90 20];
handles.make.String = {'Select Brand','Toyota','Chevy','Honda','Ford','Dodge','Jeep'};
handles.model = uicontrol('Style','popupmenu','Enable','off');
handles.model.Position = [25 325 90 20];
handles.model.String = {'Select Model'};
handles.c = uicontrol('Style','popupmenu','Enable','off');
handles.c.Position = [25 300 90 20];
handles.c.String = {'Select Engine'};
% Store handles struct in the figure
guidata(figureHandle, handles);
end
%%
function popup_menu_Callback(src,eventdata)
% Get the structure using guidata in the local function
handles = guidata(gcbo);
% Determine the selected data set.
str = src.String;
val = src.Value;
% If a make is selected, enable the next popup menu
if val > 1
handles.model.Enable = 'on';
else
handles.model.Enable = 'off';
end
% Set current data to the selected data set.
switch str{val}
case 'Toyota'
handles.model.String = {'Select Model','Tacoma','Silverado','Tundra','Colorado'};
case 'Chevy'
handles.model.String = {'Select Model','cModel1','cModel2'};
case 'Honda'
handles.model.String = {'Select Model','hModel1','hModel2'};
otherwise
handles.model.String = {'Select Model'};
handles.model.Enable = 'off';
end
% Save the change you made to the structure
guidata(gcbo, handles);
end
Thank you this is extremely helpful and much appreciated.
So I have worked through and added a pushbutton command that I am looking to load a file based upon the last drop down selected. How can you load variables from another .m file to the current worspace when pressing the button. For ex. when the last drop down tab is 'blue' the pushbutton when activated will load all the variables of filename('blue_variables') and if the last tab is 'red' it will load 'red_variables'. Thank you
The only way to add variables from a script is to run the script.
Another option is to save the variable(s) to a .mat file. Then have your button load the mat file, which loads the variables to the workspace.
I tried doing it as a .mat file and using the same scenario as above with switch cases for the pushbutton based on the last drop down but it gave me an error that the variable src. was unknown.
How are you using src?
Keep in mind variable scope. You are loading a mat file in a function. None of those variables will exist once the function finishes running.
I currently have it like this
function load_file_pushbutton_Callback(hObject, eventdata, handles)
handles = guidata(gcbo);
% Determine the selected data set.
str = src.String;
val = src.Value;
% If a engine is selected, enable the next popup menu
if val > 1
handles.data.Enable = 'on';
else
handles.data.Enable = 'off';
end
% Set current data to the selected data set.
switch str{val}
case '4.0 V6'
handles.data.String = load('Toyota_Dual_VVT_i_1GR_FE_Engine_Specs','-mat');
otherwise
handles.data.Enable = 'off';
end
end
Is there a way to have the button load the file without using a function.
Not in a GUI. Code execution all occurs in functions.
Mat files load variables to the workspace, not to a variable. If you want to capture them in the handles structure, you'd have to know what each of those variable names is going to be and manually assign them to the handles structure.
load('Toyota_Dual_VVT_i_1GR_FE_Engine_Specs.mat');
handles.data.var1 = var1;
handles.data.var2 = var2;
...
I'm curious. What are we designing this for? If this is a class project, I'm tempted to step back and let you figure some things out on your own.
It is a personal project that I opted to take up on that will then be used as a class project. I appreciate the help I am just new to the GUI controls of MATLAB.

Sign in to comment.

More Answers (0)

Categories

Products

Community Treasure Hunt

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

Start Hunting!