using 'WindowMousePress' with 'ButtonDownFcn'?

6 views (last 30 days)
CM
CM on 17 Sep 2025
Commented: CM on 23 Sep 2025 at 4: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.

Accepted Answer

Snehal
Snehal on 23 Sep 2025 at 4:01
Hi @CM,
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:
  1. 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
  2. Inside the ‘ButtonDownFcn’ callback, enable the corresponding figure’s ‘WindowButtonMotionFcn’ and ‘WindowButtonUpFcn’ to handle the actual drag and release logic
  3. Finally, disable those figure callbacks once the mouse is released
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!

More Answers (0)

Categories

Find more on Startup and Shutdown in Help Center and File Exchange

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!