Buttondownfcn not working on slider

I am making a simple real-time data viewer with buttons such as play, pause and slider using the MATLAB GUI. After the user presses the playback slider, it is necessary to update each step (50 samples per second). This functionality interferes with the manual slider engine (you have to move it to 1/50 of the second). At the moment, I have installed the slider update every 50 times (so once per second), and it works fine if you do not hold the slider longer than required for the update.

The problem is that if the Slider Enable property is enabled, Buttondownfcn does not work for left-clicking (this is done for the right one). Using Buttondownfcn, I could block the update and completely solve the problem. Is there any way around this?

% --- Executes on slider movement. function slider_Callback(hObject, eventdata, handles) disp('Slider movement') % --- Executes on button down. function slider_ButtonDownFcn(hObject, eventdata, handles) disp('Button down') 
+6
source share
1 answer

You can achieve interrupt playback by setting the Enable property of your slider to off or inactive when you press the play button, and using the ButtonDownFcn function, which stops playing and sets Enable to on .

Using togglebutton as my play button (other control widgets should work as long as you can keep the boolean flag somewhere accessible), I used the following as a Callback for the button:

 function playcallback(toggle_button, ~, slider_) set(slider_, 'Enable', 'inactive'); %slider is disabled while get(toggle_button, 'Value') %Value is used as flag for playing current_value = get(slider_, 'Value'); set(slider_, 'Value', rem(current_value + 0.01, 1)); %shift slider (looping) pause(1/50); end set(slider_, 'Enable', 'on'); %done playing, turn slider back on end 

And for the slider: ButtonDownFcn :

 function stopslide(~, ~, toggle_button) %play flag off: in playcallback, the while loop stops, %the slider is enabled and the playcallback function returns set(toggle_button, 'Value', 0); end 

You can register these callbacks as follows:

 set(toggle_button_handle, 'Callback', {@playcallback, slider_handle}); set(slider_handle, 'ButtonDownFcn', {@stopslide, toggle_button_handle}); 

Caution: if you start adding other widgets that interact with the slider / play button, similarly, you will increase the likelihood of race conditions appearing.

+1
source

Source: https://habr.com/ru/post/909807/


All Articles