why doesn't uidropdown doesn't seem to work correctly (NOT in guide or app designer)?

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.
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

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
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).
Thanks for pointing that out , I didn't see that. I think this might be an issue with my local windowing system because I'm running on Linux. I know the example code isn't supposed to do anything. the issue is that on my end, browsing over an option in the dropdown menu and clicking it doesn't make it show up as the selected option in the figure in the closed dropdown menu. e.i. starting with the closed pulldown menu, with the default option (red), if I open the menu and hover over "green" and then click, it should change the text in the bar when the menu closes to indicate the selected value, but the selection remain "red"
before opening menu.
with dropdown open, green is clicked.
selection has not changed.
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?
I've tried the simplest code possible:
fig = uifigure;
dd = uidropdown('Items',{'option1', 'option2', 'option3'});
it will still not let me click on anything other than the default option, even without any callback.
dd.Value is always "option1"
I think this is relevent. The following loads on the terminal when MATLAB is started:
MATLAB is selecting SOFTWARE OPENGL rendering.
MESA-LOADER: failed to open radeonsi: /usr/lib/dri/radeonsi_dri.so: cannot open shared object file: No such file or directory (search paths /usr/lib/x86_64-linux-gnu/dri:\$${ORIGIN}/dri:/usr/lib/dri, suffix _dri)
failed to load driver: radeonsi
MESA-LOADER: failed to open zink: /usr/lib/dri/zink_dri.so: cannot open shared object file: No such file or directory (search paths /usr/lib/x86_64-linux-gnu/dri:\$${ORIGIN}/dri:/usr/lib/dri, suffix _dri)
failed to load driver: zink
MESA-LOADER: failed to open kms_swrast: /usr/lib/dri/kms_swrast_dri.so: cannot open shared object file: No such file or directory (search paths /usr/lib/x86_64-linux-gnu/dri:\$${ORIGIN}/dri:/usr/lib/dri, suffix _dri)
failed to load driver: kms_swrast
MESA-LOADER: failed to open swrast: /usr/lib/dri/swrast_dri.so: cannot open shared object file: No such file or directory (search paths /usr/lib/x86_64-linux-gnu/dri:\$${ORIGIN}/dri:/usr/lib/dri, suffix _dri)
failed to load swrast driver.
I will try this again on Windows and see if i can get it to work. This is not not the first time I've had a problem with OpenGL and MATLAB running on Linux.

Sign in to comment.

Answers (1)

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:
  1. Try re-installing or updating the appropriate graphics drivers.
  2. 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

Products

Release

R2023a

Asked:

on 4 Aug 2023

Answered:

on 22 Aug 2023

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!