GUI Popup Menu and Listbox Default

15 visualizaciones (últimos 30 días)
MeEngr
MeEngr el 25 de Nov. de 2014
Comentada: MeEngr el 26 de Nov. de 2014
Hi all,
I'm using a listbox and a popup menu in my Matlab GUI. They both have 3 items/options each.
Now when I run the GUI, the default item showing on the popup menu is option 1. The default item highlighted in the listbox is also number 1.
The problem is, these values which I think should be the default values are not used in the program. Some random values are chosen. Sometimes option number 1, sometimes 2, sometimes 3. How do I fix this? I would like the program to run using the option appearing in the popup menu and the one highlighted in the listbox, since naturally the user thinks these options will be used if they want to use them and they don't select them when they run the program as they're already selected. Can anyone help please?
Thanks.

Respuesta aceptada

Geoff Hayes
Geoff Hayes el 25 de Nov. de 2014
Scott - if you want the (default or user-selected) values from the list box and popup menu to be used in the code/program, then use the handles for these two controls/widgets to get that data. If you are using GUIDE to design your GUI, then use the handles structure which "manages" handles to all of the controls to obtain the data. For example, if you have a push button that executes some code based on the selections in the listbox (tagged as listbox1) and the popup menu (tagged as popupmenu1), then your push button callback could look something like
function pushbutton1_Callback(hObject,eventdata,handles)
% get the selection from the listbox
lbItems = get(handles.listbox1,'String');
lbSelection = lbItems{get(handles.listbox1,'Value')}
% get the selection from the popup menu
pmItems = get(handles.popupmenu1,'String');
pmSelection = pmItems{get(handles.popupmenu1,'Value')}
% do something with the selections
Note that since you are selecting items from a cell array of strings (either lbItems or pmItems) then if you are expecting numeric data, you would have to convert it accordingly.
If you are not using GUIDE, then you can just follow the above using the appropriate handles from when you created the controls.
Try the above and see what happens!

Más respuestas (1)

Image Analyst
Image Analyst el 25 de Nov. de 2014
I have no idea why it's selecting random indexes for your popup and listbox (it never does that for me), but you can set them to what you want. First, unless you specified options in the string property of GUIDE, you'll need to write and call a function to load them up. For example in my OpeningFcn function I call a function I wrote called LoadUpListBox(handles). This loads up the listbox with a bunch of images in the folder I want.
So, once you have the listbox and popup loaded with items, you can specify the item you want to be the default. For example let's say you want item #2 in the listbox to be preselected and item #3 in the popup to be selected. In your OpeningFcn, after the controls are loaded, you set the selection with set():
set(handles.listbox1, 'Value', 2);
set(handles.popup1, 'Value', 3);

Categorías

Más información sobre Migrate GUIDE Apps en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by