How to find the item order number in List Box in App designer?
Show older comments
I have a List box named "Numbers" that has the same 4 items, they all are the number 5. How can I pick the Item3 (The third in order) to change it to 3.
I used [~,idx] = ismember(app.NumbersListBox.Value,app.NumbersListBox.Items);
But I alwyas get to change the first on because the Value are the same.
How can I find inx using "Item number" instead of "Value" ?
Thank you so much
14 Comments
Adam
on 17 Sep 2019
Surely if you already know the index then just use it where you would use the 'idx' that you extract above (which is always 1). You should just be able to index into the list of items with this.
Faez Alkadi
on 17 Sep 2019
Adam
on 17 Sep 2019
You said you want item 3. That is the 3rd element of the listbox. What were you going to do with 'idx' from your above code? Just use 3 instead of idx as it will be the 3rd item. I haven't really used list boxes in App designer, but at a guess
app.NumbersListBox.items{3}
will return this.
Ankit
on 17 Sep 2019
Hallo Faez,
When your listbox have same values for e.g.
app.NumberListBox.Items = {'5', '5', '5', '5'};
[~,idx] = ismember(app.NumbersListBox.Value,app.NumbersListBox.Items);
The value of idx is always 1.
In case your list box have diff values for e.g. app.NumberListBox.Items = {'1', '2', '3', '4'};
[~,idx] = ismember(app.NumbersListBox.Value,app.NumbersListBox.Items);
The value of idx is selected item.
You have specific use case where all the values are same?
Faez Alkadi
on 17 Sep 2019
Edited: Faez Alkadi
on 17 Sep 2019
Adam
on 17 Sep 2019
How can you have a general case if all the list items are the same? You have to pick up the index from somewhere, whether you hard-code it as 3 or get it from somewhere else, but the idea is still the same.
Ned
on 18 Sep 2019
How does your app determine which of the numbers to change? user input? Because like Adam said, if you're programming it, you can just select Item 3. otherwise, I'm thinking you could use something along the lines of
find(ismember(app.NumbersListBox.Items,app.NumbersListBox.Value));
to get the index of all the items indicated by ListBox.Value
Faez Alkadi
on 18 Sep 2019
Ned
on 18 Sep 2019
Have you considered using itemsData to index the entries?

This way value always gives you the index, instead of the item.
Faez Alkadi
on 19 Sep 2019
Ned
on 20 Sep 2019
Just have whatever code you have that updates ListBox.Items, also update ListBox.ItemsData with the next index value.
Faez Alkadi
on 20 Sep 2019
Faez Alkadi
on 22 Sep 2019
Seonggon Kim
on 18 Feb 2023
If there are items with same name, is there a way out?
I recommand to use the 'Table' instead 'List Box'.
app.table.Selection
Answers (0)
Categories
Find more on Develop Apps Using App Designer in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!