Clear Filters
Clear Filters

Figure's 'WindowButtonUpFcn' gets 'overridden'

7 views (last 30 days)
Hi,
I'am relatively new to MATLAB and got a problem concerning the figure's 'WindowButtonUpFcn' if you want to use it for modifying several plots on the same figure.
Preface: I've got a figure whereon I plot several graph plots (using subplot) along with additonal marker plots to identify characteristic spots on the individual curves. After the plotting is done the user can manually adjust the locations of the automatic found marker spots by mouse-dragging. Before the dragging occurs the 'XLim' and 'YLim' of the current axis are changed to perform a 'zoom-in'. A control variable is used to check if the zoom was done and needs to be reversed.
Here is some reduced code to show my problem:
figureCBProb.m
function figureCBProb()
clc;
fHandle = figure(1);
drawPlot1(fHandle);
drawPlot2(fHandle);
end
drawPlot1.m
function drawPlot1(fHandle)
x = -2*pi:0.05:+2*pi;
subplot(1,2,1);
hplot1 = plot(x, sin(x));
hold on;
hPoint(1) = plot(0, sin(0), 'bx', 'ButtonDownFcn', @startDrag);
checkProcess = 0;
defaultYLimits = -1;
defaultXLimits = -1;
set(fHandle, 'WindowButtonUpFcn', @stopDrag);
function startDrag(varargin)
disp('Start dragging: Plot1!');
set(fHandle, 'WindowButtonMotionFcn', @doDrag);
defaultXLimits = get(gca, 'XLim');
defaultYLimits = get(gca, 'YLim');
end
function doDrag(varargin)
disp('Do some dragging: Plot1');
currentPoint = get(gca, 'CurrentPoint');
if(checkProcess == 0)
disp('Zoom-Process: Plot1');
set(gca, 'XLim', [currentPoint(1)-2 currentPoint(1)+2]);
set(gca, 'YLim', [currentPoint(3)-0.5 currentPoint(3)+0.5]);
checkProcess = 1;
end
end
function stopDrag(varargin)
disp('Dragging stopped: Plot1!');
set(fHandle, 'WindowButtonMotionFcn', '');
if(checkProcess == 1)
disp('Undo Zoom-Process: Plot1');
set(gca, 'XLim', defaultXLimits);
set(gca, 'YLim', defaultYLimits);
checkProcess = 0;
end
end
end
drawPlot2.m
function drawPlot2(fHandle)
x = -2*pi:0.05:+2*pi;
subplot(1,2,2);
hplot2 = plot(x, cos(x));
hold on;
hPoint(2) = plot(pi/2, cos(pi/2), 'bx', 'ButtonDownFcn', @startDrag);
checkProcess = 0;
defaultYLimits = -1;
defaultXLimits = -1;
set(fHandle, 'WindowButtonUpFcn', @stopDrag);
function startDrag(varargin)
disp('Start dragging: Plot2!');
set(fHandle, 'WindowButtonMotionFcn', @doDrag);
defaultXLimits = get(gca, 'XLim');
defaultYLimits = get(gca, 'YLim');
end
function doDrag(varargin)
disp('Do some dragging: Plot2');
currentPoint = get(gca, 'CurrentPoint');
if(checkProcess == 0)
disp('Zoom-Process: Plot2');
set(gca, 'XLim', [currentPoint(1)-2 currentPoint(1)+2]);
set(gca, 'YLim', [currentPoint(3)-0.5 currentPoint(3)+0.5]);
checkProcess = 1;
end
end
function stopDrag(varargin)
disp('Dragging stopped: Plot2!');
set(fHandle, 'WindowButtonMotionFcn', '');
if(checkProcess == 1)
disp('Undo Zoom-Process: Plot2');
set(gca, 'XLim', defaultXLimits);
set(gca, 'YLim', defaultYLimits);
checkProcess = 0;
end
end
end
Problem: If there is only a single graph plot (commenting out drawPlot2) everything works fine. By adding the other graph plot the stopDrag function of drawPlot1 is always 'overridden' by the stopDrag function of drawPlot2 and so the zooming in plot1 is never reversed.
Is there a way to get around this issue? I tried to work with global variables of the axis handles to detect which axis has been clicked on but it is ugly and makes things more unstructured/ complicated. I'm sure there is a much better MATLAB way. :)
Furthermore I don't want to rewrite this whole 'startDrag', 'doDrag' and 'stopDrag' for every single plot but instead write it once and apply it to each single plot. But that's another problem. ;)
Thanks in advance!

Accepted Answer

Titus Edelhofer
Titus Edelhofer on 11 Aug 2011
Hi,
without trying, I guess you are nearly there: I would move the set(fHandle, 'WindowButtonUpFcn',@stopDrag) call into the startDrag function. This way you would always have the correct stopDrag for each individual plot, just like you do with the motion function...
Titus
  1 Comment
BF83
BF83 on 11 Aug 2011
Thanks for the explanation! That solved my problem perfectly.

Sign in to comment.

More Answers (0)

Categories

Find more on Graphics Object Properties in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!