필터 지우기
필터 지우기

GUI Popup Menu and Listbox Default

조회 수: 9 (최근 30일)
MeEngr
MeEngr 2014년 11월 25일
댓글: MeEngr 2014년 11월 26일
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.

채택된 답변

Geoff Hayes
Geoff Hayes 2014년 11월 25일
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!
  댓글 수: 1
MeEngr
MeEngr 2014년 11월 26일
Thanks a lot, Geoff! :)

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Image Analyst
Image Analyst 2014년 11월 25일
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);

카테고리

Help CenterFile Exchange에서 Migrate GUIDE Apps에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by