www.gusucode.com > mbcguitools 工具箱 matlab 源码程序 > mbcguitools/xregrbline.m

    function newlim = xregrbline(xyaxis,startpos,hAx)
%XREGRBLINE - a bit like rbbox, but in x or y direction only
%
%  XREGRBLINE(xyaxis) - waits for mouse press on current axis
%  XREGRBLINE(xyaxis, startpos)
%
%    xyaxis - a string 'x' and 'y' specifying which direction to draw line
%    startpos - 4 element vector [x y width height]

%  Copyright 2000-2013 The MathWorks, Inc. and Ford Global Technologies, Inc.


% get all the axis/figure settings
if nargin<3
    hAx = gca;
end
hFig = ancestor(hAx, 'figure');

if nargin<2 || isempty(startpos)
    pt = get(hAx, 'CurrentPoint');
    startpos = [pt(1,1:2), 0,0];
end

lineH = i_drawline(hAx, startpos, xyaxis);

oldmotionfcn = get(hFig,'WindowButtonMotionFcn');
set(hFig, 'WindowButtonMotionFcn', {@i_motiontracker, hAx, lineH, xyaxis});
bm = xregGui.ButtonUpManager(hFig);
bm.getNextEvent({@i_finishline, lineH});

waitfor(lineH(1), 'userdata'); % wait for button up

% Restore the motion function
set(hFig, 'WindowButtonMotionFcn', oldmotionfcn);

l = lineH(2);
x = get(l, 'XData');
y = get(l, 'YData');

switch xyaxis
    case 'x'
        newlim = [min(x), max(x)];
    case 'y'
        newlim = [min(y), max(y)];
end

% get rid of the lines
delete(lineH);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function lineH = i_drawline(hAx, startpos, xyaxis)

ydelta = 0;
xdelta = 0;
X = startpos(1);
Y = startpos(2);
width = startpos(3);
height = startpos(4);

[deltax,deltay] = i_calcdelta(hAx);

switch xyaxis
    case 'x'
        ydelta = deltay;
        height = 0;
    case 'y'
        xdelta = deltax;
        width = 0;
end

% draw lines

% left hand end
linepts = [X-xdelta X+xdelta; Y-ydelta, Y+ydelta];
lineH(1)  = line('Parent',hAx,...
    'XData',linepts(1,1:2),...
    'YData',linepts(2,1:2));

% the line
linepts = [X, X+width; Y Y+height];
lineH(2)  = line('Parent',hAx,...
    'XData',linepts(1,1:2),...
    'YData',linepts(2,1:2));

% the rh end
linepts = [X+width-xdelta, X+width+xdelta; Y+height-ydelta, Y+height+ydelta];
lineH(3)  = line('Parent',hAx,...
    'XData',linepts(1,1:2),...
    'YData',linepts(2,1:2));


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function i_updateline(lineH, newPos, xyaxis)

oldX = get(lineH(2), 'XData');
oldY = get(lineH(2), 'YData');

switch xyaxis
    case 'x'

        addX = newPos(1);
        newX = [oldX, addX];

        % remove x points that are outside the range (oldX(1), newX)
        startX = oldX(1);
        minX = min(startX, addX);
        maxX = max(startX, addX);
        removeX = (newX<minX) | (newX>maxX);
        removeX(1) = 0;
        removeY = false(size(newX));
        newY = [oldY, oldY(1)];
        set(lineH(3), 'XData', [newX(end), newX(end)]);
    case 'y'

        addY = newPos(2);
        newY = [oldY, addY];

        % remove y points that are outside the range (oldY(1), newY)
        startY = oldY(1);
        minY = min(startY, addY);
        maxY = max(startY, addY);
        removeY = (newY<minY) | (newY>maxY);
        removeY(1) = 0;
        removeX = false(size(newY));
        newX = [oldX, oldX(1)];
        set(lineH(3), 'YData',[newY(end), newY(end)]);
end

newX(removeX) = []; newY(removeX) = [];
newX(removeY) = []; newY(removeY) = [];

set(lineH(2), 'XData', newX, 'YData', newY);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function i_finishline(src, evt, lineH)
set(lineH(1), 'UserData', 1);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function i_motiontracker(src, evt, hAx, lineH, xyaxis)
if all(isgraphics(lineH))
    currPos = get(hAx,'CurrentPoint');
    currPos = currPos(1,1:2);
    i_updateline(lineH, currPos, xyaxis);
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [deltax,deltay] = i_calcdelta(hAx)

oldunits=get(hAx,'Units');
set(hAx,'Units', 'pixel');
apos = get(hAx,'Position');
set(hAx,'Units', oldunits);

deltapix  = 5;
deltax = (deltapix*diff(get(hAx,'XLim')))/apos(3);
deltay = (deltapix*diff(get(hAx,'YLim')))/apos(4);