- Use ‘ButtonDownFcn’ instead of 'WindowButtonDownFcn' for detecting when a particular plot object was clicked since this is a more direct approach and you won’t have to manually check the object being clicked that way
- Inside the ‘ButtonDownFcn’ callback, enable the corresponding figure’s ‘WindowButtonMotionFcn’ and ‘WindowButtonUpFcn’ to handle the actual drag and release logic
- Finally, disable those figure callbacks once the mouse is released
using 'WindowMousePress' with 'ButtonDownFcn'?
조회 수: 5 (최근 30일)
이전 댓글 표시
Hi, I am considering altering my code, which currently uses 'WindowButtonDownFcn', 'WindowMousePress', 'WindowButtonMotionFcn' and 'WindowButtonUpFcn' to track the mouse when the user clicks and holds (drags) on a plot in a uifigure. I am considering going with the "more modern" approach from this link:
https://au.mathworks.com/matlabcentral/answers/458264-using-windowbuttondownfcn-in-app-designer
which recommends using the 'ButtonDownFcn' attached to each plot instead of the 'WindowButtonDownFcn' attached to the figure. This would make a lot of the handling easier. However, as far as I can tell, there is no equivalent to 'WindowMousePress', 'WindowButtonMotionFcn' and 'WindowButtonUpFcn' if I go down that route.
Is that correct?
I don't think a hybrid system is going to help me either.
댓글 수: 0
채택된 답변
Snehal
2025년 9월 23일
I understand that you want to move away from figure-level callbacks and use the “more modern” approach of directly implementing functions like ‘ButtonDownFcn’ for individual plot objects.
As you rightly mentioned, there are no object-level alternatives for 'WindowMousePress', 'WindowButtonMotionFcn' and 'WindowButtonUpFcn' callback functions. However, you can consider using the following approach to achieve the overall functionality in a hybrid manner:
This will enable you to achieve the continuous drag behaviour while simultaneously reducing the number of times figure-wide callbacks are called
You can refer to the following sample code snippet for more idea on how this can be implemented:
% In your UIFigure startup or setup code:
p = plot(app.UIAxes, rand(1,10), 'ButtonDownFcn', @(src,evt) startDrag(app, src));
% Callback functions
function startDrag(app, src)
% Store which object was clicked
app.DragObject = src;
% Enable motion and release callbacks on the figure
app.UIFigure.WindowButtonMotionFcn = @(fig,evt) doDrag(app, evt);
app.UIFigure.WindowButtonUpFcn = @(fig,evt) endDrag(app, evt);
end
function doDrag(app, evt)
cp = app.UIAxes.CurrentPoint;
% Example: move the clicked object vertically with mouse Y
app.DragObject.YData(:) = cp(1,2);
end
function endDrag(app, evt)
% Disable motion and release callbacks when mouse is released
app.UIFigure.WindowButtonMotionFcn = '';
app.UIFigure.WindowButtonUpFcn = '';
app.DragObject = [];
end
Hope this helps!
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Startup and Shutdown에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!