Need help understanding the popup menu in GUI.

2 次查看(过去 30 天)
I could use some clarification regarding the proper use of the popup menu in a GUI. Say I have a popup menu that has 'Option A' and 'Option B'. When I run the code, the figure is generated and the popup menu shows 'Option A'. However, the GUI doesn't recognize the popup menu until I select either Option A or Option B from the list.
Furthermore, when I try to call the current value of the popup menu (after it recognizes it) it returns all values, so it lists a the whole cell array defining the options... {'Option A', 'Option B'}.
My questions are this:
1. How can I initialize the popup menu so that it recognizes this input from launch?
2. How can I save and call just the current selection in the popup menu?
I have attached the files for reference.

采纳的回答

Geoff Hayes
Geoff Hayes 2015-10-15
Matt - use the Value property of the pop-up menu control to set and get the index of the selected item. For example, to default the menu to the second item in the list, copy the following into the OpeningFcn of your GUI (I'm assuming that you are using GUIDE to create your GUI)
set(handles.popupmenu1,'Value',2);
So now, when the GUI is launched, the second item will be selected. Now, to get the index of the item that has been selected, do the following (in whatever callback that is appropriate)
idx = get(handles.popupmenu1,'Value');
Try the above and see what happens!
  3 个评论
Matt Brown
Matt Brown 2015-10-16
Actually.... I am still having an issue. I am able to call up whatever value is shown after an initial selection, but the default value is still not getting used at launch. I have boiled this down to a much simpler GUI that basically displays whatever selection you have. What I would like to be able to do is have this display 'A' when the program launches, then A, B, C or D once they are selected from the popup menu.
See attached help.m file.
What am I piece of this puzzle am I missing?
Geoff Hayes
Geoff Hayes 2015-10-16
Matt - given the code, I see that you set the first element in the list as
set(handles.popupmenu1,'Value',1)
so you should see A as the selected element in the popup menu when you launch the GUI. You then mention ...when the program launches, then A, B, C or D once they are selected from the popup menu.... This statement suggests that you wish to allow the user to select A even though it has already been selected. What are you expecting to happen immediately when the user launches the GUI? If the default setting for this menu is A do you wish to have some code fire that would be the same as if the user had selected A from the menu? (Since A is already selected then the user won't be able to select it until this option has changed.)

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Environment and Settings 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by