- In the Component Browser, click on the app node.
- In the right-hand pane, scroll down to the "Callbacks" section and click on "Add StartupFcn". This will create a new function called startupFcn.
Problem with value property of uidropdown
6 views (last 30 days)
Show older comments
Hi,
I'm using a uidropdown control with the Items and ItemsData properties set within the method startupFcn of my app. I'm using the following simple code to specify the items of the dropdown control along with numeric IDs:
app.MethodDropDown.Items = Methods.TYPES(:, 1);
app.MethodDropDown.ItemsData = [Methods.TYPES{:, 2}];
Methods.TYPES is a cell-array in class Methods with the first column containing character arrays (i.e. the names for the method types) and the second column contains numeric ID values for the method types.
When retrieving the value of the selected item I observe a different behavior between different MATLAB versions. Up to 2022b the Value property returns the numeric value of the selected item as specified in ItemsData. However, starting with 2023a, a string (i.e. in my case a character array) is returned.
Am I doing something wrong? I suspect that something changed between 2022b and 2023a but I can't find anything about such a change in the changelog of MATLAB. Moreover, the documentation of 2023a still states that my code seems to be correct.
Best,
Michael
0 Comments
Answers (1)
Pavan Sahith
on 30 Jul 2024
Edited: Pavan Sahith
on 30 Jul 2024
Hello Michael,
It appears that you're encountering an issue with the 'uidropdown' control in MATLAB R2023a,
I use the same version and attempted to reproduce the problem using a simplified example. Here are the steps I followed and the code that worked correctly for me, returning the numerical ID as expected:
1.Added the startupFcn:
function startupFcn(app)
% Define the Methods.TYPES cell array
Methods.TYPES = {
'Method1', 1;
'Method2', 2;
'Method3', 3;
};
% Set the items and items data for the dropdown
app.MethodDropDown.Items = Methods.TYPES(:, 1);
app.MethodDropDown.ItemsData = [Methods.TYPES{:, 2}];
end
2. I inserted a button to retrive the value and added a callback to it.
% Button pushed function: RetrieveValueButton
function RetrieveValueButtonPushed(app, event)
selectedValue = app.MethodDropDown.Value;
% Display the selected value
disp(selectedValue);
end
you can refer to this MathWorks documentation to know more : uidropdown
5 Comments
Pavan Sahith
on 1 Aug 2024
Edited: Pavan Sahith
on 1 Aug 2024
Good to know ,
yeah the issue is reproducible with the code provided.
I also noticed that in MATLAB R2023b, there's an update that allows us to access the index of the component value in the list of items using the ValueIndex property.
I believe this might be helpful in the meantime
See Also
Categories
Find more on Interactive Control and Callbacks 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!