You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
Get selection index for DropDown object using AppDesigner
124 views (last 30 days)
Show older comments
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?
Accepted Answer
Matt J
on 10 Nov 2017
Edited: Matt J
on 10 Nov 2017
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.
15 Comments
Ahmed
on 28 Aug 2019
I've tried alot but it's not working. Could you please elaborate with an example?
Jim Hokanson
on 29 Aug 2019
Edited: Jim Hokanson
on 16 Feb 2021
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 (EDIT: on the same line, 1 entry per line makes them strings) 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.
Darren Akehurst
on 24 Oct 2019
This really needs to be added to the documentation. Documentation is not at all clear about this
Kristoffer Walker
on 27 Apr 2020
Designers,
I like the ability to have Value point to the string in the Items array. However, there are many times in code where it is useful to have Value point to "Items" and several lines later having Value point to an Index array. Why not simply take the three Value, Items, and ItemsData and replace them with Value and ValueIndex, both of which refer to the Items array? I miss the older functionality, which seems more logical and easier to use.
Kris
Erik
on 16 Feb 2021
You wrote
" 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."
Unfortunately when entered this way, itemdata will become a cell array of strings
{'1','2','3','4'}
if 'Option 1' is selected then
disp(app.DropDown.Value*1)
will display 49 since char(49) = '1'
However, if we first set it to a numerical array
app.DropDown.ItemsData = 1:numel(app.DropDown.Items);
then disp(app.DropDown.Value*1) will display 1
Jim Hokanson
on 16 Feb 2021
I think this is the first time I've been @ mentioned on MATLAB answers :)
Weird, I wonder if MATLAB changed its behavior since I last tested. If you enter the values on one line it works but indeed it seems that multiline entry forces the entries to be strings. Regardles, at some point typing 1,2,3,etc. may get annoying and simply setting ItemsData as you suggest at run time may be more desirable.
Erik
on 18 Feb 2021
Hmm, (R2019b) Update 5
If I fill in 1,2,3,4 for ItemData on the first line, then ItemsData = { '1,2,3,4'}
So only one item and hence only the first option is selectable. So the Callback gets never triggered.
Strangely, when you edit the Value, it give the numbers 1 2 3 4 on separate lines.

Seems like a bug to me
Jim Hokanson
on 18 Feb 2021
Edited: Jim Hokanson
on 18 Feb 2021
I just tested in 2020b and 1,2,3,4 produces an array of numbers, not a single ellement cellstr
For what it is worth, I found the app designer to be too much. It may be getting better over time and I can't remember what the tipping point was (different figure type?), but I find GUIDE to be sufficient. I only use GUIDE for layout, not code generation. I create a handle class that loads the GUI and the handles on construction and go from there ...

Mario Malic
on 18 Feb 2021
Edited: Mario Malic
on 18 Feb 2021
What is the purpose of setting up your cell array of numeric data to be cell array of character data? It will only cause confusion.
Value can take only one argument, not more, and it has to be one of Items.
Regarding this comment:
if 'Option 1' is selected then
disp(app.DropDown.Value*1)
will display 49 since char(49) = '1'
I think this was recently mentioned on MATLAB frustrations question, how does one even multiply character '1' with 1? There are also other examples related to it, but if you know you will be multiplying the DropDown Value, you shouldn't be using characters.
Erik
on 19 Feb 2021
@Mario Malic Maybe you missed the point.
This string array versus numerical array salad was not intended, but I got into it by the unexpected and confusing behaviour of the appdesigner dropbox editor in version 2019b. @Jim HokansonShowed that it is fixed in version 2020b.
Erik
on 19 Feb 2021
How did you actually make the screen shot with the command window query of app?
Are you in debug mode? I do not see the K> prompt.
Or is the app object public or is it somehow in the global workspace?
Mario Malic
on 19 Feb 2021
Edited: Mario Malic
on 19 Feb 2021
TLDR on the bottom.
This behaviour is standard I guess, snippet below is ran in 2020b
a = 'b'
a*2
ans =
196
- If your expand your Items, they will appear only in one row which means that it has only one element
- Can you check if in your ItemsData, each entry is in its own row? If yes, that will cause the data to be treated as cell array of character arrays. Even if it consists only of numeric values, those will be treated as characters
An example, how you'd write things in the App Designer:
Items = opt1, opt2, opt3
ItemsData = 1 2 3
% Items will be 1x1 cell array, whereas items will be 1x3 numeric array
Items = opt1, opt2,
opt3
% NOTE <-- Sometimes users like to finish the entry with 'Enter' button so we have an empty row like here
ItemsData = 1
2
3
% Items and ItemsData will both be 3x1 cell array of character arrays, 3rd Item will be empty
Items = opt1, opt2
ItemsData = 1, 2, 3
% same note as above, an empty row
% Items will be 1x1 cell array, whereas ItemsData will be 2x1 cell array of character values,
% Since Items(1) refers to ItemsData(1), ItemsData(1) will appear in Value
On the last case, which you showed on the picture, if I expand the Value box, I see the same, except in 2020b other rows are a shifted by space like this.
1
2
3
TLDR: Check for empty rows in your Items and ItemsData.
To get the access to app in your command window, you can call your app directly
app = NameOfTheApp;
If you have opened the app already and you want to get the access, there's property called RunningAppInstance in App Designer figure. If you have more figures open, then use app tags to differentiate your app from other figures.
app = get(findall(0,'type', 'figure'), 'RunningAppInstance');
Erik
on 19 Feb 2021
Thanks on the info on how to get access to a running app.
There is no way in my version to enter the ItemData so that it becomes a numerical array. What ever I do in de compent inspector, the end result is the same (see below). And yes the (single) Value is shown in the inspector as numbers under each other while the ItemData is shown as 1,2,3,4
But, like I said, in the 2020b version, this behaviour fortunately changed apparently
>> app.DropDown
ans =
DropDown (Option 1) with properties:
Value: '1 2 3 4'
Items: {'Option 1' 'Option 2' 'Option 3' 'Option 4'}
ItemsData: {'1 2 3 4'}
Editable: 'off'
ValueChangedFcn: ''
Position: [131 192 100 22]
More Answers (0)
See Also
Categories
Find more on Introduction to Installation and Licensing in Help Center and File Exchange
Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)