MATLAB Answers


Get selection index for DropDown object using AppDesigner

Asked by Jim Hokanson on 10 Nov 2017
Latest activity Commented on by Darren Akehurst on 24 Oct 2019
Using AppDesigner, how do I access the selected index of a DropDown menu. The Value is the selected string but I can't find anywhere that indicates the selected index. Instead I seem to have to do:
I = find(strcmp(h.Items,h.Value));
Is this really the suggested approach?


Sign in to comment.

1 Answer

Answer by Matt J
on 10 Nov 2017
Edited by Matt J
on 10 Nov 2017
 Accepted Answer

The h.Value property has different behavior depending on how h.Items and h.ItemsData are set up. If you define the ItemsData property to be some sequence of numeric indices i=1:N, then h.Value will be the numeric value of i corresponding to the selection.


Show 1 older comment
In the AppDesigner, in the Drop-Down editor, there are 3 entries: Value, Items, and ItemsData. By default, ItemsData is not populated, which means that Value takes on the Items value. However, if you manually enter ItemsData, then the selection takes on the value in ItemsData.
So by default, in my version of Matlab, I get Items = {'Option 1'} {'Option 2'} {'Option 3'} {'Option 4'}
So when selecting the second option, the value becomes 'Option 2', which in some cases may not be ideal. Now if you enter in 1,2,3,4 (each on its own line) in the ItemData field, now the Value for the 2nd selection will be 2.
Alternatively you can enter whatever you want so that when a selection is made, you get that particular ItemData entry. So for example 12,34,59,36 when the 2nd element is selection (Option 2), then you'll get 34 as the value.
Thanks alot. Now it works.
This really needs to be added to the documentation. Documentation is not at all clear about this

Sign in to comment.