how to get index of selected item in listbox of appdesginer
Mostrar comentarios más antiguos
Hi:
I want to get the index of selected item in listbox of appdesigner.
I found a solution here: https://www.mathworks.com/matlabcentral/answers/376091-index-of-the-items-in-a-listbox
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 comentarios
Adam Danz
el 31 de Ag. de 2019
Why have duplicate options in the listbox? What's the difference between apples and apples? How could it possibly matter which "apples" is selected?
Maybe with some additional background info we could suggest an alternative but there doesn't seem be much sense in duplicate listbox values.
Yu Li
el 31 de Ag. de 2019
Adam Danz
el 31 de Ag. de 2019
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.
Respuesta aceptada
Más respuestas (2)
Ivan Demec
el 9 de Mayo de 2025
I landed here looking the exact same thing, but then by accident figured out that matlab (since don't know which version) has this built in w ValueIndex property
fig = uifigure();
lbx = uilistbox(fig,'Items',{'Apples','Apples','Pears','Bananas'}, ...
'ItemsData',{'Apples1','Apples2','Pears1','Bananas1'});
lbx.ValueIndex
Marvin Seifert
el 19 de Nov. de 2019
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 comentarios
The ValueChangedData structure (mouseclick event) does not contain the selection index.
event.Value provides the same information as the app.ListBoxHandle.Value data described in the other answer here.
The app.ListBoxHandle.Value data is also available in any callback function within the app, not just within the ValueChange function.
Here's a functional demo that can be run from an m-file that demonstrates this. The ValueChangedFcn function displays the results of event.Value and hObj.Value (r2019b).
fig = uifigure();
lbx = uilistbox(fig,'Items',{'Apples','Apples','Pears','Bananas'}, ...
'ItemsData',{'Apples1','Apples2','Pears1','Bananas1'});
lbx.ValueChangedFcn = @listboxValChgFcn;
function listboxValChgFcn(hObj, event)
disp(['From event: ', event.Value])
disp(['From Object: ', hObj.Value])
%within app designer the obj handle would be something like app.ListBox.Value
end
Results when items are selected from listbox
From event: Apples2
From Object: Apples2
From event: Pears1
From Object: Pears1
From event: Bananas1
From Object: Bananas1
Marvin Seifert
el 19 de Nov. de 2019
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
el 19 de Nov. de 2019
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.
Categorías
Más información sobre Develop Apps Programmatically en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!