- 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'?
20 次查看(过去 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!