Reputation: 1433
I'm making a simple real time data viewer with buttons like play, pause and slider using MATLAB GUI. After the user presses play slider needs to be updated every step (50 samples per second). That functionality is interfering with manually moving slider (you'd have to move it in 1/50th of the second). For now I've set updating of the slider every 50th time (so once every second) and it works fine, unless you hold the slider longer then it takes to update.
The problem is that if slider property Enable is on, Buttondownfcn doesn't work for left click (it does for right one). Using Buttondownfcn I would be able to lock the updating and solve the problem completely. 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')
Upvotes: 6
Views: 1358
Reputation: 633
You can achieve interrupt play by setting the Enable
property of your slider to off
or inactive
when you press your play button and using a ButtonDownFcn
function that stops play and sets Enable
back to on
.
Using a togglebutton
as my play button (other control widgets should work as long as you can save a boolean flag somewhere accessible), I used the following as 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 the following as ButtonDownFcn
for the slider:
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 like this:
set(toggle_button_handle, 'Callback', {@playcallback, slider_handle});
set(slider_handle, 'ButtonDownFcn', {@stopslide, toggle_button_handle});
Caveat: if you start adding other widgets interacting with the slider/play button in a similar manner to this, you increase your chance of introducing race conditions.
Upvotes: 1