crosshair.m

来自「含有多种ICA算法的eeglab工具箱」· M 代码 · 共 953 行 · 第 1/3 页

M
953
字号
    switch move,    case 'nextx',        % Increase current xindex by one        if(interp == 1), H.data.xindex = H.data.xindex + 1; end    case 'prevx',        % Decrease current xindex by one        if(interp == 1), H.data.xindex = H.data.xindex - 1; end    case 'nexty', H.data.yindex = H.data.yindex + 1;    case 'prevy', H.data.yindex = H.data.yindex - 1;    case 'ygt',        ydata = H.data.ydata(H.data.xindex,:);        [ysort,yi] = sort(ydata);        currYI = find(ysort == H.data.ypoint);        if currYI < length(yi),            H.data.yindex = yi(currYI+1);        end    case 'ylt',        ydata = H.data.ydata(H.data.xindex,:);        [ysort,yi] = sort(ydata);        currYI = find(ysort == H.data.ypoint);        if currYI > 1,            H.data.yindex = yi(currYI-1);        end    otherwise    end        H = checkdatarange(H);        % Get x/y value at new x/y index    H.data.xpoint = H.data.xdata(H.data.xindex,H.data.yindex);    H.data.ypoint = H.data.ydata(H.data.xindex,H.data.yindex);        set(H.interp,'Value',1);    H = updateGUI(H);    return%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%function [ H ] = checkdatarange(H),    % Ensure that x/y index is within data range    s = size(H.data.xdata,1);    if( H.data.xindex < 1 ),        H.data.xindex = 1;    elseif( H.data.xindex >= s ),        H.data.xindex = s;    end    s = size(H.data.ydata,2);    if( H.data.yindex < 1 ),        H.data.yindex = 1;    elseif( H.data.yindex >= s ),        H.data.yindex = s;    endreturn%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%function [ H ] = updateDATA( H )    % Only update if mouse pointer is in the    % axis limits    set(H.gui,'units','normalized');    axpos = get(H.axis,'position');    figcp = get(H.gui,'Currentpoint');    axlim = axpos(1) + axpos(3);    aylim = axpos(2) + axpos(4);    if or(figcp(1) > (axlim+.01), figcp(1) < (axpos(1)-.01)),        return;    elseif or(figcp(2) > (aylim+.01), figcp(2) < (axpos(2)-.01)),        return;    end        CurrentPoint  = get(H.axis,'Currentpoint');    H.data.xpoint = CurrentPoint(1,1);    H.data.ypoint = CurrentPoint(1,2);        doNearTrace   = get(H.traceNearest,'Value');        if (doNearTrace > 0)                % Get new yindex for nearest trace        [ H.data.xpoint, ...          H.data.xindex, ...          H.data.ypoint, ...          H.data.yindex ] = NearestXYMatrixPoint( H.data.xdata,...                                                  H.data.ydata,...                                                  H.data.xpoint,...                                                  H.data.ypoint);    else        H.data.yindex = get(H.trace,'Value');    end        CurrentPoint  = get(H.axis,'Currentpoint');    H.data.xpoint = CurrentPoint(1,1);    H.data.ypoint = CurrentPoint(1,2);        H = interpY(H);    H = updateGUI(H);return%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%function [ H ] = updateGUI( H )    InterpMethod = get(H.interp,'Value');    if (InterpMethod > 1)        % There is no specific matrix x-index for         % an interpolated point, but the nearest xindex        % is always returned from the interp function below        % so that next/prev move function works correctly        set(H.xindex,'String','interp');    else        set(H.xindex,'String',num2str(H.data.xindex));    end    set(H.xindex,'Value',H.data.xindex);        tracestr = sprintf('%d',H.data.yindex);    set(H.yindex,'String',tracestr,'Value',uint16(H.data.yindex));    set(H.trace,'Value',uint16(H.data.yindex));        % Create the crosshair lines on the figure, crossing at the x,y point    x_rng  = get(H.axis,'Xlim');    y_rng  = get(H.axis,'Ylim');    set(H.xline,'Xdata',[H.data.xpoint H.data.xpoint],'Ydata',y_rng);    set(H.yline,'Ydata',[H.data.ypoint H.data.ypoint],'Xdata',x_rng);        % Update the x,y values displayed for the x,y point    xstring = sprintf('%14.6f',H.data.xpoint);    ystring = sprintf('%14.6f',H.data.ypoint);    set(H.xvalue,'String',xstring,'Value',H.data.xpoint);    set(H.yvalue,'String',ystring,'Value',H.data.ypoint);        % Calculate the f(x) function    fyeq = get(H.fyeq,'string');    fyval = eval(strrep(fyeq,'y',ystring));    fystr = sprintf('%14.6f',fyval);    set(H.fyvalue,'String',fystr,'Value',fyval);        % Calculate the f(y) function    fxeq = get(H.fxeq,'string');    fxval = eval(strrep(fxeq,'x',xstring));    fxstr = sprintf('%14.6f',fxval);    set(H.fxvalue,'String',fxstr,'Value',fxval);    return%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%function [ H ] = interpY( H )        % In this function, xdata & ydata are arrays, not matrices    xdata = H.data.xdata(:,H.data.yindex);    ydata = H.data.ydata(:,H.data.yindex);        if      H.data.xpoint >= max(xdata)                            H.data.xpoint  = max(xdata);            H.data.xindex  = find(xdata == max(xdata));            H.data.ypoint  = ydata(H.data.xindex);            return;    elseif  H.data.xpoint <= min(xdata)            H.data.xpoint  = min(xdata);            H.data.xindex  = find(xdata == min(xdata));            H.data.ypoint  = ydata(H.data.xindex);            return;    end        % 'none|nearest|linear|spline|cubic'    interp = get(H.interp,'Value');        switch interp    case 1        % Given that xdata & ydata are the same length arrays,        % we can find the ypoint given the nearest xpoint.        [H.data.xindex, H.data.xpoint] = NearestXYArrayPoint( xdata, H.data.xpoint );        H.data.ypoint = ydata(H.data.xindex);    case 2        H.data.ypoint = interp1( xdata, ydata, H.data.xpoint, 'nearest' );    case 3        H.data.ypoint = interp1( xdata, ydata, H.data.xpoint, 'linear' );    case 4        H.data.ypoint = interp1( xdata, ydata, H.data.xpoint, 'spline' );    case 5        H.data.ypoint = interp1( xdata, ydata, H.data.xpoint, 'cubic' );    otherwise        %use default (linear in matlabR12)        H.data.ypoint = interp1( xdata, ydata, H.data.xpoint );    endreturn%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%function [ Yall ] = interpYall( H )        xdata = H.data.xdata(:,H.data.yindex);    Yall  = H.data.ydata;        if      H.data.xpoint >= max(xdata),            H.data.xpoint  = max(xdata);            H.data.xindex  = find(xdata == max(xdata));            Yall = ydata(:,H.data.xindex);            return;    elseif  H.data.xpoint <= min(xdata),            H.data.xpoint  = min(xdata);            H.data.xindex  = find(xdata == min(xdata));            Yall = ydata(:,H.data.xindex);            return;    end        % 'none|nearest|linear|spline|cubic'    interp = get(H.interp,'Value');        switch interp,    case 1        % do nothing in this case    case 2        Yall = interp1( xdata, Yall, H.data.xpoint, 'nearest' );    case 3        Yall = interp1( xdata, Yall, H.data.xpoint, 'linear' );    case 4        Yall = interp1( xdata, Yall, H.data.xpoint, 'spline' );    case 5        Yall = interp1( xdata, Yall, H.data.xpoint, 'cubic' );    otherwise        %use default (linear in matlabR12)        Yall = interp1( xdata, Yall, H.data.xpoint );    endreturn%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%function [ index, point ] = NearestXYArrayPoint( data_array, point, type )        if ~exist('type','var') type = ''; end    % In this function, input data_array is an array, not a matrix.    % This function returns the data point in the array    % that has the closest value to the value given (point).  In    % the context of 'crosshair' the point is a mouse position.        if      point >= max(data_array)            point  = max(data_array);            index  = find(data_array == point);            return;    elseif  point <= min(data_array)            point  = min(data_array);            index  = find(data_array == point);            return;    end        data_sorted = sort(data_array);        greater = find(data_sorted > point);    greater_index = greater(1);        lesser = find(data_sorted < point);    lesser_index = lesser(length(lesser));        greater_dif = data_sorted(greater_index) - point;    lesser_dif  = point - data_sorted(lesser_index);        if strcmp(type,'nextx'),        index = greater_index;    elseif strcmp(type,'prevx'),        index = lesser_index;    else        if (greater_dif < lesser_dif)            index = find(data_array == data_sorted(greater_index));        else            index = find(data_array == data_sorted(lesser_index));        end    end    point = data_array(index);    return%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%function [ xpoint, xindex, ypoint, yindex ] = NearestXYMatrixPoint( Xdata, Ydata, xpoint, ypoint )    % In this function, Xdata & Ydata are matrices of the same dimensions.    % This function attempts to find the nearest values in Xdata & Ydata    % to the mouse position (xpoint, ypoint).        % It is assumed that Xdata has identical columns, so we only really    % need the first column to find the nearest value to xpoint.        [ xindex, xpoint ] = NearestXYArrayPoint( Xdata(:,1), xpoint );        % Now, given the xpoint, we can select just that row of the    % Ydata matrix corresponding to the xpoint.    ydata = Ydata(xindex,:);        % The ydata array is searched in same manner as the xdata    % array for the nearest value.    [ yindex, ypoint ] = NearestXYArrayPoint( ydata, ypoint );    return%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%function [H] = INIT    H.gui  = gcf; % Get current figure handles    H.axis = gca; % Get current axis handles        H.axis = gca; % Get current axis handles    H.ylim = get(H.axis,'ylim');        % store current button fcn    H.button = get(H.gui,'WindowButtonDownFcn');    % set XHR button down fcn    set(H.gui,'WindowButtonDownFcn','crosshair(''down'');');    set(H.gui,'KeyPressFcn','crosshair(''keypress'');');        % Match background figure colour    bgcolor = get(H.gui,'Color');    % Try to adapt the foreground colour a little

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?