MATLAB: How to get index of selected item in listbox of appdesginer

duplicate valuesduplicatesindex of selected item in listbox of appdesginerlistbox

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

Best Answer

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'