필터 지우기
필터 지우기

how to get index of selected item in listbox of appdesginer

조회 수: 98 (최근 30일)
Yu Li
Yu Li 2019년 8월 30일
댓글: Adam Danz 2019년 11월 19일
Hi:
I want to get the index of selected item in listbox of appdesigner.
The weakness of the solution provided here using 'ismember' function is that, if I have two items that are totally the same, the returned index is not unique. so, is there any other solutions?
Thanks!
Yu
  댓글 수: 3
Yu Li
Yu Li 2019년 8월 31일
I understand your concern-having two variables with totally the same value in a list box may not be necessary.
but I want know how to get the actual selected item's location, in case this situation occurs.
Adam Danz
Adam Danz 2019년 8월 31일
The problem isn't that duplicate values are unnecessary. The problem is that there should be no difference between "apples" and "apples" so selecting either of them should result in the same outputs. Otherwise, how would the user know which "apples" to select?
In any case, I added an answer that suggests a workaround.

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

채택된 답변

Adam Danz
Adam Danz 2019년 8월 31일
편집: Adam Danz 2019년 8월 31일
Continuing from comments under the question.
There is unfortunately no property of a listbox that returns the index of the selected item. The only way to get the index is by finding the selection within a list of options by using strcmp() or ismember() etc. If your list of options has duplicates and one of the duplicates was selected, there is no way to match which of the duplicates was selected.
One potential workaround in app designer is to assign unique values to the ItemsData property. When the ItemsData property is set, the Value property of a listbox returns the ItemsData selection rather than the Items selection.
Example:
fig = uifigure();
lbx = uilistbox(fig,'Items',{'Apples','Apples','Pears','Bananas'}, ...
'ItemsData',{'Apples1','Apples2','Pears1','Bananas1'});
Now when the 2nd "Apples" is selected,
lbx.Value
ans =
'Apples2'
  댓글 수: 2
Yu Li
Yu Li 2019년 8월 31일
Thanks for your reply and answer.
Bests,
Yu
Adam Danz
Adam Danz 2019년 8월 31일
Glad I could help!

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

추가 답변 (1개)

Marvin Seifert
Marvin Seifert 2019년 11월 19일
You cant get the index directly from the listbox class but you can get the index from the mousclick event. In the callback function for the listbox you can do
indices = event.Value;
This works for multiple selections as well.
  댓글 수: 3
Marvin Seifert
Marvin Seifert 2019년 11월 19일
Yes you are right, what I had in mind was this, to get the index rather than a unique string:
Items = {'Apples','Apples','Pears','Bananas'};
nr_Items = numel(Items);
fig = uifigure();
lbx = uilistbox(fig,'Items',Items, ...
'ItemsData',(1:nr_Items));
lbx.ValueChangedFcn = @listboxValChgFcn;
function listboxValChgFcn(hObj, event)
idx = event.Value %This is the index as a double
disp(['From event: ', num2str(event.Value)])
disp(['From Object: ', num2str(hObj.Value)])
%within app designer the obj handle would be something like app.ListBox.Value
end
Adam Danz
Adam Danz 2019년 11월 19일
Yes, with the ItemsData you can output the pre-established index. But if there is an error in the numbering of the the ItemsData, the true index will not be returned. But your method of indexing the number of list items is a good approach.

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

카테고리

Help CenterFile Exchange에서 Develop uifigure-Based Apps에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by