Dans MATLAB, comment pouvez-vous avoir une exécution de rappel alors qu'un curseur est en train d'être glissé?
-
14-11-2019 - |
Question
J'ai créé une interface graphique MATLAB à l'aide du guide.J'ai un curseur avec une fonction de rappel.J'ai remarqué que ce rappel, qui est censé exécuter «sur le mouvement du curseur», ne fonctionne que lorsque le curseur a été déplacé et la souris libérée.
Y a-t-il un moyen d'obtenir un script à exécuter au fur et à mesure que le curseur est traîné, pour la mise à jour en direct d'une parcelle?Je suppose que je suppose qu'il faut faire quelque chose pour arrêter le script à courir trop de fois.
La solution
Even though the callback of the slider isn't being called as the mouse is moved, the 'Value'
property of the slider uicontrol is being updated. Therefore, you could create a listener using addlistener
that will execute a given callback when the 'Value'
property changes. Here's an example:
hSlider = uicontrol('Style', 'slider', 'Callback', @(s, e) disp('hello'));
hListener = addlistener(hSlider, 'Value', 'PostSet', @(s, e) disp('hi'));
As you move the slider you should see 'hi'
being printed to the screen (the listener callback), and when you release the mouse you will see 'hello'
printed (the uicontrol callback).
Autres conseils
Just for the record, this subject is discussed in detail here: http://UndocumentedMatlab.com/blog/continuous-slider-callback/ - several alternative solutions are presented there. gnovice's solution using addlistener
is equivalent to the handle.listener alternative
, since addlistener
is basically just a wrapper for the latter.
If you want to execute the same original callback you passed to uicontrol
you can add this generic listener which bootstraps the existing callback:
sld.addlistener('Value','PostSet',@(src,data) data.AffectedObject.Callback(data.AffectedObject,struct('Source',data.AffectedObject,'EventName','Action')));