How to detect (programmatically induced) value changes of uidropdown?
19 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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?
0 commentaires
Réponse acceptée
Walter Roberson
le 20 Avr 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.
Plus de réponses (0)
Voir également
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!