필터 지우기
필터 지우기

How to select different options in the same listbox and save to thier respective variables?

조회 수: 1 (최근 30일)
I want to select 3 different items from the same list box and save it to 3 different handles.
opt1 = handles.opt1;
opt2 = handles.opt2;
opt3 = handles.opt3;
opt = [opt1,opt2,opt3];
But when I select three different strings, I get only the 'opt3' as 'opt' output. I don't want to create 3 list boxes for the same options and make the GUI look messy. How do I do it? Please Help

채택된 답변

Adam Danz
Adam Danz 2018년 7월 12일
편집: Adam Danz 2018년 7월 13일
The example below shows how to isolate the selected items using the callback function to the listbox. If you produced the listbox using GUIDE, focus on the callback function in my example and skip the first half that produces the listbox.
% Create listbox
function myList
c = dialog();
listbox1 = uicontrol('Parent',c,...
'Style','listbox',...
'Position',[90 90 100 100],...
'Min', 0, ...
'Max',3,...
'String',{'Lancaster', 'Cincinnati', 'Sofia', 'Rochester'},...
'Callback', @listbox1_Callback);
% Callback function
function listbox1_Callback(hObject, eventdata)
mySelection = hObject.String(hObject.Value);
if length(mySelection)<3
return
end
opt1 = mySelection{1};
opt2 = mySelection{2};
opt3 = mySelection{3};
*Note that you'll need to adapt the callback function if the user is allowed to select less or more than 3 options.
  댓글 수: 14
Vinayak Appasaheb Bhatte
Vinayak Appasaheb Bhatte 2018년 7월 20일
Also, I had one more issue. I posted the question but no one replied.I always see the top option in the listbox is default selected. How to remove this default selection?
Please help me. I checked the property inspector but couldn't find any solution. Thanks!
Adam Danz
Adam Danz 2018년 7월 20일
I just replied to your question in your original thread.

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

추가 답변 (0개)

카테고리

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