Borrar filtros
Borrar filtros

How to detect (programmatically induced) value changes of uidropdown?

14 visualizaciones (últimos 30 días)
Situation:
I am currently creating a UI with some dropdown menus that are dynamically populated (based on hardware devices that are connected to the PC). I might change the selectable items and the value programmatically without a user action required.
I want to trigger the execution of a function on the change of the value of the dropdown menu.
I am aware of the ValueChangedFcn callback. However this callback is only executed when the change is made by the user via the UI and not by programmatically induced changes to the value, as one can read in the documentation (This callback function executes when the user selects a different option from the drop-down list. It does not execute if the Value property changes programmatically).
What does not work:
Typically I would add an event listener but the property Value is not SetObservable so this does not work.
Subclassing the class to register a custom set method is also not possible cause uidropdown is sealed.
In my case I always know when I change the value programatically and I could trigger the desired function manually but this does not seem right to me because I then need use the ValueChngedFcn for user changes, add the function every time I change the value and/or write a custom function that changes the value and then automatically executes the function.
All of that seems not right because I would really like to bind the triggering of the function to the change of the value itself and not the cause of it.
Am I misusing uidropdown?
Do I misuse the dropdown menu to store a value and should rather have a separate variable for it that will be changed when the user makes a selection in the dropdown menu?
When using a separate variable I need to take special care to properly initalize that variable and the dropdown menu so that it reflects the value in the dropdown manu at all times.
>> What are your ideas to solve this problem in an elegant way?

Respuesta aceptada

Walter Roberson
Walter Roberson el 20 de Abr. de 2024
I am aware of the ValueChangedFcn callback. However this callback is only executed when the change is made by the user via the UI and not by programmatically induced changes to the value
... Unless you trigger the ValueChangedFcn yourself.
event = struct('Value', NewValue, 'PreviousValue', app.NAMEOFDROPDOWN.Value, ...
'Edited', 1, 'Source', app.NAMEOFCOMPONENT, 'EventName', 'ValueChanged');
app.NAMEOFDROPDOWN.ValueChangedFcn(app, event);
where app.NAMEOFDROPDOWN is the uidropdown and where app.NAMEOFCOMPONENT is the name of the component that is triggering the callback.
  1 comentario
Michael
Michael el 22 de Abr. de 2024
Together with a custom method that first sets the property and then triggers the event this did the job.
Thank you for this very helpful hint!!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Develop Apps Using App Designer en Help Center y File Exchange.

Productos


Versión

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by