why doesn't uidropdown doesn't seem to work correctly (NOT in guide or app designer)?
Show older comments
I know you are supposed to look through documentation for basic questions like this, but everything I've found is extremely confusing and unhelpful. Most of the tutorials out there also assume that if you are wanting to create graphical user interface elements that you are working in app designer, which I am not, and to my understanding has a completely different syntax for working with these, so they aren't helpful either. All I want to do is simple, have a dropdown element and access whatever is currently selected or entered, but this seems to be impossible.
For example, the documentation for using the uidropdown function https://www.mathworks.com/help/matlab/creating_guis/write-callbacks-for-apps-created-programmatically.html#brqow8p
says to save and run this as a .m file, but the example doesn't seem to work the way other ui objects do. It DOES is produce a figure with a dropdown menu as you would expect, but if you select a different element it does not update, the selected item just reverts back to the default value when you let go of the menu.
function updateDropDown
fig = uifigure('Position',[500 500 300 200]);
dd = uidropdown(fig, ...
'Editable','on', ...
'Items',{'Red','Green','Blue'}, ...
'ValueChangedFcn',@addItems);
end
function addItems(src,event)
val = event.Value;
if event.Edited
src.Items{end+1} = val;
end
end
for any other uielement, I can simply access object.Value to access whatever that element has been set at, and if the selection changes that variable changes, but it seems to be impossible to change uidropdown's object.Value.
I gather the answer has to do with callbacks somehow, but I cannot find a good tutorial or explanation for these, they all seem to assume you are using app designer. for example:
why are some callbacks written as @(src, event) callback (src, event) whereas others are
@callback(src, event)
what is src and what is event? these are never previously defined variables, they seem to just be something you have to add in for syntax reasons, but what actually are they? The syntax for using callbacks also seem to be inconsistent. the uicontrol function seems to be very different from any of the others.
Is there a more detailed documentation for using callbacks on ui elements OUTSIDE of app editor for this other than the official one that someone could point me to? because that does not do a good job explaining anything. And why can't I make even a simple dropdown menu that actually lets me interact?
5 Comments
Cris LaPierre
on 4 Aug 2023
Edited: Cris LaPierre
on 4 Aug 2023
I'm having a little trouble following you. Let me state what I understant.
You have a dropdown with values Red, Green, and Blue. By default, red is seletect. If you change the selection, you would like to query the dropdown object's Value property and get the selected item. So if you change the selection to 'Green', dd.Value should return 'Green'?
I tested your code with a slight modification so that I could access dd, and that's what it does.
fig = uifigure('Position',[500 500 300 200]);
dd = uidropdown(fig, ...
'Editable','on', ...
'Items',{'Red','Green','Blue'}, ...
'ValueChangedFcn',@addItems);
function addItems(src,event)
val = event.Value;
if event.Edited
src.Items{end+1} = val;
end
end

dpb
on 5 Aug 2023
I'm confused about what your intent is for the dropdown -- the callback tries to add a new element to the list but the example callback from which you seem to have taken the pattern looks to as though simply letting the user edit in the dropdown changes the list without physically adding it. I've got other stuff in workspace at the moment so it isn't convenient to try to see how it behaves, but I'd suggest running their example as it is written first and try the option of adding a new image to the list and see if that isn't what happens without anything else. Their example uses it to display another image; yours doesn't do anything with it, though, so what's the point?
As for the complaint about the documentation, the link you posted has as the very first titled section, a concise definition of the callback arguments including the naming convention used...
Callback Function Arguments
When a UI component executes a callback function, MATLAB automatically passes two input arguments to the function. These input arguments are often named src and event. The first argument is the UI component that triggered the callback. The second argument provides event data to the callback function. The event data that it provides is specific to the callback property and the component type. To determine the event data associated with a callback property, see the properties page of the UI component that executes the callback.
Study the sections on alternative ways to specify the callback function and, with patience and diligence the differences in syntax for anonymous functions and referring to existing functions will become clear (or at least less confusing).
Andrew Morris
on 5 Aug 2023
Rik
on 5 Aug 2023
I'm on mobile so searching the documentation is harder (as is testing it), but is there a reason you're using ValueChangedFcn instead of Callback? And have you tried setting breakpoints in your callback function to see what is happening with the variables?
Andrew Morris
on 6 Aug 2023
Answers (1)
Suman Sahu
on 22 Aug 2023
Hi Andrew,
I have tried running your code on my machine(Windows) and there does not seem to be any issue with the code. I looked at your comments, you are working on a Linux machine and the issue is apparently with your graphics drivers. Here are some troubleshooting steps that you can follow to resolve your issue:
- Try re-installing or updating the appropriate graphics drivers.
- OpenGL configuration: Try to run MATLAB with hardware OpenGL rendering if software OpenGL is being used and try software OpenGL rendering if hardware OpenGL is being used.
You can check the current rendering implementation using the following function in MATLAB command window:
Info = rendererinfo(gca)
To start MATLAB session with hardware OpenGL use following command in your system’s command prompt or terminal:
matlab -nosoftwareopengl
To start MATLAB session with software OpenGL use:
matlab -softwareopengl
Categories
Find more on App Building 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!

