Continuously pressing GUI slider arrow causes hanging
1 回表示 (過去 30 日間)
古いコメントを表示
based on the following question: https://es.mathworks.com/matlabcentral/answers/49905-continuously-pressing-gui-slider-arrow-causes-hanging
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
0 件のコメント
採用された回答
Rik
2022 年 4 月 20 日
編集済み: Rik
2022 年 4 月 22 日
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 件のコメント
Rik
2022 年 4 月 29 日
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.
その他の回答 (0 件)
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!