⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 brushscatter.m

📁 常用ROBUST STATISTICAL
💻 M
📖 第 1 页 / 共 2 页
字号:
function brushscatter(arg,labs)
%   BRUSHSCATTER    Scatterplot Brushing and Linking
%
%   BRUSHSCATTER(X,LABS)
%   X is an n x p matrix of data values. LABS is an optional input argument
%   containing the variable labels. This must be a p-dimensional cell array
%   of strings.
%
%   To craete a brush: put the cursor in any scatterplot. Hold the left
%   mouser button and drag to create a brush. 
%
%   To brush points: Click on the brush outline, hold the left mouse button
%   and drag the brush.
%
%   For a menu of other options: Click on any diagonal plot using the righ
%   mouse button. A menu of options will appear.
%
%
%   Exploratory Data Analysis Toolbox, V2, December 2006
%   Martinez and Martinez, Exploratory Data Analysis with MATLAB
%   CRC Press

if nargin==2
    % First one contains data - initialize figure.
    X = arg;
    arg = 'init';
    [n,p] = size(X);
    if length(labs) ~= p
        error('Array of variable names is wrong dimensionality')
        return
    end
    if ~iscell(labs)
        error('Array of variable names must be a cell array of strings.')
        return
    end
elseif nargin==1 && isnumeric(arg)
    % Called only with the data. Initialize the figure and use generic
    % variable names.
    X = arg;
    arg = 'init';
    % Get some generic names.
    [n,p] = size(X);
    for i = 1:p
        labs{i} = ['Variable ' int2str(i)];
    end
end   
    

if strcmp(arg,'brushplot')
    tg = findobj('tag','brushscatter');
    H = get(tg,'userdata');
    
    % Execute button from brush frame
    if ~isempty(H.Hbrush)
        resetplots
    end
    brushplot
    
elseif strcmp(arg,'init')
    % create the scatterplot
    init(X,labs)
    
elseif strcmp(arg,'createbrush')
    tg = findobj('tag','brushscatter');
    H = get(tg,'userdata');
    
    CreateBrushButtDwn(H,tg)
    
elseif strcmp(arg,'BrushButtDwn')
    % User has clicked on the brush.
    % Set the function for brush motion.
    tg = findobj('tag','brushscatter');
    H = get(tg,'userdata');
    set(H.fig,'WindowButtonMotionFcn','brushscatter(''movebrush'')')
    % Set the function for when the user clicks up on the brush.
    set(H.fig,'WindowButtonUpFcn','brushscatter(''movebrushbuttup'')')
    set(H.fig,'Pointer','fleur');
    
elseif strcmp(arg,'movebrush')
    % When brush moves - call update function.   
    MoveBrush
    
elseif strcmp(arg,'movebrushbuttup')
    % User finished moving the brush - let up on the button.
    % Reset the windows functions.
    movebrushbuttup

elseif strcmp(arg,'reset')
    % This resets all plots to their original color. 
    % used with all frames
    resetplots
    
elseif strcmp(arg,'delbrush')
    % This deletes the brush.
    % Gets rid of the brush handles, etc.
    delbrush

%%%%%%%%%%%%%%%%%%%%  from here %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%    
% elseif strcmp(arg,'delete')
%     % Operation is to delete points. Need to reset the menu items.
%     % Also need to re-do the axes, after the point(s) is deleted.
%     % this operation only makes sense with the 'lasting' mode.
%     % Delete won't really take into account the mode.
%     tg = findobj('tag','brushscatter');
%     H = get(tg,'userdata');
%     H.operation = 'delete';
%     set(H.MenDel,'checked','on')
%     set(H.MenHigh,'checked','off')
%     set(H.fig,'userdata',H)
    
% elseif strcmp(arg,'highlight')
%     % Operation is to highlight points. Change the marker to filled 
%     % and another color - maybe red. Adjust menu items. This operation
%     % can be any of the 3 modes. 
%     tg = findobj('tag','brushscatter');
%     H = get(tg,'userdata');
%     H.operation = 'highlight';
%     set(H.MenHigh,'checked','on')
%     set(H.MenDel,'checked','off')
%     set(H.fig,'userdata',H)

elseif strcmp(arg,'color')
    % Change the color.
    tg = findobj('tag','brushscatter');
    H = get(tg,'userdata');
    tmp = uisetcolor('Choose the color:');
    if length(tmp) ~= 1
        H.color = tmp;
    end
    set(H.fig,'userdata',H)

elseif strcmp(arg,'transient')
    % Set the flag to transient.
    % Set the check in menu to transient.
    % Set the others to uncheck.
    tg = findobj('tag','brushscatter');
    H = get(tg,'userdata');
    set(H.MenTrans,'checked','on')
    H.mode = 'transient';
    set([H.MenLast,H.MenUndo],'checked','off')
    set(H.fig,'userdata',H)
  
elseif strcmp(arg,'lasting')
    % SAB, except for lasting.
    tg = findobj('tag','brushscatter');
    H = get(tg,'userdata');
    set(H.MenLast,'checked','on')
    H.mode = 'lasting';
    set([H.MenTrans,H.MenUndo],'checked','off')
    set(H.fig,'userdata',H)
    
elseif strcmp(arg,'undo')
    % SAB, except for undo.
    tg = findobj('tag','brushscatter');
    H = get(tg,'userdata');
    set(H.MenUndo,'checked','on')
    H.mode = 'undo';
    set([H.MenTrans,H.MenLast],'checked','off')
    set(H.fig,'userdata',H)
    
elseif strcmp(arg,'resetfig')
    % Reset the figure to initial state.
    tg = findobj('tag','brushscatter');
    H = get(tg,'userdata');
    resetplots
    
%%%%%%%%%%%%   TO HERE  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%  were added back from the old brushscatter to the new version
        
elseif strcmp(arg,'close')
    % in other gui's we will do some housekeeping. With this gui, we do not
    % have to do so. Obviously, the user will want to keep the data from
    % the loadgui for other applications. That is the purpose.
    tg = findobj('tag','brushscatter');
    H = get(tg,'userdata');
    ud = get(0,'userdata');
    resetplots
    movebrushbuttup
    Haxs = get(ud.brushptr,'children');
    for i = 1:length(Haxs)
        if iscell(Haxs)
            set(Haxs{i},'buttondown','')
        else
            set(Haxs(i),'buttondown','')
        end
    end
    delete(tg)
    
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function brushplot
% This brings up the selected plot to brush.
ud = get(0,'userdata');
if isempty(ud.X)
    errordlg('You must load up some data and plot first.')
    return
end
if isempty(ud.brushptr)
    errordlg('You must have some plots open to brush.')
    return
end
tg = findobj('tag','brushscatter');
H = get(tg,'userdata'); % userdata for this gui.
% Reset everything - including the buttondown function for axes
resetplots
Haxs = get(ud.brushptr,'children');
for i = 1:length(Haxs)
    if iscell(Haxs)
        set(Haxs{i},'buttondown','')
    else
        set(Haxs(i),'buttondown','')
    end
end
% Get the brush mode: 1 = transient, 2 = lasting.
mode = get(H.popmode,'value');
if mode == 1
    H.mode = 'transient';
elseif mode == 2
    H.mode = 'lasting';
else
    H.mode = 'undo';    % this un-highlights individual points.
end
% Get the plot to brush. This SHOULD be index to the brush pointer.
bplot = get(H.popplot,'value');
figure(ud.brushptr(bplot))
Hax = get(ud.brushptr(bplot),'children');
% This figure is made active. ONLY this plot has the ability to draw a
% brush.
set(Hax,'buttondown','brushscatter(''createbrush'')','drawmode','fast')
% Reset the handle to the current brushable plot.
H.brushplot = ud.brushptr(bplot);
set(tg,'userdata',H)

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function resetplots
% Reset all plots to their original color - black
% Hgui = findobj('tag','brushscatter');
% H = get(Hgui,'userdata');

hndls = findobj('tag','high');
delete(hndls)

% for ii = 1:length(ud.highlight)
%     set(ud.highlight(ii),'xdata',nan,'ydata',nan)
% end

% % delete the brush
delbrush

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function delbrush
Hgui = findobj('tag','brushscatter');
H = get(Hgui,'userdata');
ud = get(0,'userdata');
if ~isempty(H.Hbrush) & ishandle(H.Hbrush)
    delete(H.Hbrush);
end
H.Hbrush = [];
set(Hgui,'userdata',H)
movebrushbuttup
% Reset everything - including the buttondown function for axes
% Haxs = get(ud.brushptr,'children');
% for i = 1:length(Haxs)
%     if iscell(Haxs)
%         set(Haxs{i},'buttondown','')
%     else
%         set(Haxs(i),'buttondown','')
%     end
% end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function groupcolor(H)
% This is executed when the person clicks on 'execute' button for Color the
% Groups.
% H is the structure to the brushscatter userdata

ud = get(0,'userdata');
% Find the group that has been selected.
grpstrg = get(H.listgroup,'string'); 
grp = get(H.listgroup,'value');
if strcmp(grpstrg(grp),'None')
    errordlg('You must load up some class labels first.')
    return
end
% H.color, ud.classlab
% Find the indices to the observations belonging to the selected group.
if iscell(grpstrg)
    tmp = str2num(grpstrg{grp});
else
    tmp = str2num(grpstrg(grp));
end
inds = find(ud.classlab == tmp);
% Color the groups in the brushable plots
for ii = 1:length(ud.highlight)
    axes(ud.highlight(ii));
    % First get the data that are plotted there.
    Hline = findobj(ud.highlight(ii),'tag','black');
    Xp = get(Hline,'xdata');   % This is the full data set.
    Yp = get(Hline,'ydata');
    % Now color the group.
    Ht = line('xdata',Xp(inds),'ydata',Yp(inds),...
        'markersize',3,'marker','o','linestyle','none',...
        'tag','high',...
        'markerfacecolor',H.color);     % Placeholder for highlighted points. 
    set(Ht,'parent',ud.highlight(ii));
    
end
if ~isempty(ud.linkap)
    % set these to the right color.
    set(ud.linkap(inds,:),'color',H.color);
end


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function obscolor(H)
% This is executed when the person clicks on 'execute' button for Color the
% Observations.
% H is the structure to the brushscatter userdata
ud = get(0,'userdata');
% Find the observations that have been selected. 
obs = get(H.listcase,'value');
% Color the obs in the brushable plots
for ii = 1:length(ud.highlight)
    axes(ud.highlight(ii));
    % First get the data that are plotted there.
    Hline = findobj(ud.highlight(ii),'tag','black');
    Xp = get(Hline,'xdata');   % This is the full data set.
    Yp = get(Hline,'ydata');
    % Now color the group.
    Ht = line('xdata',Xp(obs),'ydata',Yp(obs),...
        'markersize',3,'marker','o','linestyle','none',...
        'tag','high',...
        'markerfacecolor',H.color);     % Placeholder for highlighted points. 
    set(Ht,'parent',ud.highlight(ii));
    
end
if ~isempty(ud.linkap)
    % set these to the right color.
    set(ud.linkap(obs,:),'color',H.color);
end




%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%     CreateBrushButtDwn  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function CreateBrushButtDwn(H,tg)
% H is a structure saved in tg's userdata. This is really the brushscatter
% userdata. tg is the handle to brushscatter.
% Create a brush.

⌨️ 快捷键说明

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