Tracking the coordinates of a drawn box on an axes in a GUI

6 views (last 30 days)
I'm trying to make a GUI which plots data and allows the user to draw a box to zoom in on specific parts of the data. The code is based off of Matt Tearle's code found here. The issue I'm facing is at the end of the code when adjusting the plot's axis limits. The Y limits are not being stored, even though the X limits change accordingly.
%set the button down callback
set(plotFig,'windowbuttondownfcn',@startDraw)
uiwait(plotFig)
function startDraw(src,~)
set(src,'pointer','crosshair');
%get the current point
cp = get(currentAxes,'currentpoint');
%loads the initial point and makes variables for opposite corner
x1 = cp(1,1); y1 = cp(1,2); x2 = []; y2 = [];
%draws the four sides of the box
top = line('xdata',x1,'ydata',y1,'color','k','linewidth',1);
bot = line('xdata',x1,'ydata',y1,'color','k','linewidth',1);
left = line('xdata',x1,'ydata',y1,'color','k','linewidth',1);
right = line('xdata',x1,'ydata',y1,'color','k','linewidth',1);
%set callbacks for motion and button up once button down runs
set(src,'windowbuttonmotionfcn',@moveDraw);
set(src,'windowbuttonupfcn',@endDraw);
function moveDraw(~,~)
%gets current cursor location
cp = get(currentAxes,'currentpoint');
%stores the other corner, first corner is fixed
x2 = cp(1,1); y2 = cp(1,2);
%sets the axes
set(top,'xdata',[x1 x2],'ydata',[y1 y1]);
set(bot,'xdata',[x1 x2],'ydata',[y2 y2]);
set(left,'xdata',[x1 x1],'ydata',[y1 y2]);
set(right,'xdata',[x2 x2],'ydata',[y1 y2]);
drawnow
end
function endDraw(src,~)
%removes the callbacks. this function is supposed to only run once
set(src,'pointer','arrow');
set(src,'windowbuttonmotionfcn',[]);
set(src,'windowbuttonupfcn',[]);
set(src,'windowbuttondownfcn',[]);
%this is the part which errors. y1 and y2 are 0 and 0, which causes
%the function to error. their actual values are not 0 and 0.
axis(plotHandles.zoomCutData);
uiresume(plotFig);
end
end
I'm not sure why I can't get the data to be passed between the three callbacks. Even stranger is that x1 and x2 are available in endDraw(), but y1 and y2 are not. x1 and x2 display the correct values.

Answers (0)

Categories

Find more on App Building 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!