Continuously pressing GUI slider arrow causes hanging

1 view (last 30 days)
Maruan Alberto Bracci
Maruan Alberto Bracci on 19 Apr 2022
Commented: Rik on 29 Apr 2022
The solution proposed doen't woks for me.
I've created a GUI with a slider control that update a graph. When I make distinct individual clicks of the slider arrows, it works fine, even if it takes some seconds to update the figure. When I hold the arrow buttons down continuously the figure is updated to the final value of the slider, however, matlab is still busy processing the long function repeatedly.
What appears to be happening is that by holding down the arrow, the slider callback and the corresponding slow function gets launched many times repeatedly. The BusyAction and the interrupting callback don't apply to the slow function that has already been lunched. Ultimately, it is accoumulated in the queue and keep the program busy.
Is there some way that the continuous-arrow-press condition is suppressed? Or might there be some way to force the GUI to wait until all other graphics and uicontrol data in the figure are refreshed before re-executing the callback?
Here there is part of the code to better understand the problem
S.Slider = uicontrol('style','slider', 'callback', @Slider_A); %generate the slider
function Slider_A(My_Slider, ~, ~)
S = guidata(My_Slider); % Get S struct from the figure
S.Value = get(My_Slider, 'Value');
% w = waitforbuttonpress; %this would work if exist waitforbuttonRELEASE
update(S); %slow function and plotting
guidata(My_Slider, S); % Store modified S in figure
end

Accepted Answer

Rik
Rik on 20 Apr 2022
Edited: Rik on 22 Apr 2022
I would suggest setting a flag (and storing it in the guidata struct). That way you can immediately exit the function if it is already running.
The function below implements such a flag. This function also checks whether the Value has changed since the last call, in which case it will run another time.
Since an empty array is equivalent to false, you don't even need to initialize the UserData property.
function Slider_A(My_Slider, ~)
S = guidata(My_Slider); % Get S struct from the figure
% Exit this function call.
if get(My_Slider,'UserData'),return,end
% Set the flag to exit the function call if it is called before it exits.
set(My_Slider,'UserData',true);drawnow;
while true
S=guidata(My_slider); % Reload S in case it was changed in the updater.
S.Value = get(My_Slider, 'Value');
update(S);
drawnow; % Trigger the processing of callbacks and graphics object interactions
if isequal(get(My_Slider, 'Value'),S.Value)
% Value has not changed, break the loop and exit the function.
break
end
% The value has changed, rerun the updater with the new value.
end
set(My_Slider,'UserData',false);
guidata(My_slider,S)
end
  8 Comments
Rik
Rik on 29 Apr 2022
That is the difference between the two versions: mine will run one last time, and yours will not. Other than that there isn't a fundamental difference between the two. But glad you have something you like.

Sign in to comment.

More Answers (0)

Tags

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!