function C = removeZP(C, vec,handles)
% removeZP
%   change pole/zero for eraser

%   Project: IMPACT
%   Title of Code: removeZP
%   Relevant additional documentation:
%   Developer: Carlos Gonzalez
%   External Dependencies: NA
%
%   Disclaimer
%   CAUTION: It is strongly recommended that changes to this code
%   should be avoided without first consulting with authors.
 

ptX = vec(1);
ptY = vec(2);
Data = handles.Data;
vecX = Data.zpX;
if isempty(vecX)
    return;
end
[z, p, k] = zpkdata(C);
z = z{1}; p = p{1};

hAx = gca;
hMag = handles.hMag;
if hAx==hMag(1)
    vecY = 20*log10(Data.zpMag);
else vecY = Data.zpPhase;
end
xLim = get(gca, 'XLim');
yLim = get(gca, 'YLim');

dist = abs(ptX - vecX);
[temp, ind] = sort(dist);
vecX = vecX(ind);
vecY = vecY(ind);

zpClick = 0;
for m = 1:numel(vecX)
    if checkRange(ptX, ptY, vecX(m), vecY(m), xLim, yLim)
        valX = vecX(m);
        zpClick = 1;
        break;
    end
end
if zpClick % withine eraser range
    ind = getXY(valX, z);
    if ~isempty(ind)
        z(ind) = [];
    else
        ind = getXY(valX, p);
        p(ind) = [];
    end
end
C = zpk(z, p, k);
