www.gusucode.com > mbctools 工具箱 matlab 源码程序 > mbctools/+mbcmodelview/BaseScatterPlot.m

    classdef BaseScatterPlot < mbcgui.multiview.View
    %ScatterPlot scatter plots of model diagnostics
    
    %  Copyright 2015-2015 The MathWorks, Inc. and Ford Global Technologies, Inc.
    
    properties (Dependent,SetAccess=private)
        %Axes scatter plot axes
        Axes
        %Line main scatter plot line
        Line
        %Graph2D mvgraph2d object (also ContentHandle)
        Graph2D
    end
    
    properties (SetAccess=protected)
        %YFactor selected YFactor
        YFactor = 'Residuals';
        %XFactor selected XFactor
        XFactor = '';
    end
    
    methods
        function obj = BaseScatterPlot(varargin)
        %BaseScatterPlot constructor
        obj@mbcgui.multiview.View(varargin{:});
        create(obj);
        if strcmp(obj.Visible,'on') && hasData(obj.MessageService)
            update(obj)
        end
        addMessageServiceListener(obj, 'NodeUpdated',@obj.onNodeUpdated)
       
        end
        
        function lh = get.Line(obj)
        lh = get(obj.ContentHandle,'line');
        end
        function lh = get.Axes(obj)
        lh = get(obj.ContentHandle,'axes');
        end        

        function g = get.Graph2D(obj)
        g = obj.ContentHandle;
        end
        
        function ah = printCopy(obj, fig)
        %PRINTCOPY Create a printable version of the component
        %   NEWOBJ = PRINTCOPY(H, FIG) creates and returns a handle to a new
        %   component, NEWOBJ, parented by the specified figure, FIG. The new
        %   object will be used for printing a copy of this component.
        %
        %   Normally the printable version of a component is not a complete
        %   copy of the object.  There are often controls that should not
        %   appear, such as popup menus, or components whose information needs
        %   to be transformed, such as edit boxes.
        %
        %   See also canPrint, print, printSize.
        
        
        if isgraphics(obj.Axes)
            % copy axes to new figure
            ah = copyobj(obj.Axes,fig);
            % set to default units and position so figure can be resized
            set(ah,'Units',get(fig,'DefaultAxesUnits'),...
                'Position',get(fig,'DefaultAxesPosition'));
        else
            ah = [];
        end
        end
        
        function OK = canPrint(obj)
        %canPrint can always print scatter plots
        OK = true;
        end
        
        
        function s = serializeView(obj) 
        %SERIALIZEVIEW Get serializable setup data for the view
        %
        %  OUT = SERIALIZEVIEW(OBJ) retuns a MATLAB array that contains the setup
        %  data that will allow this view to be recreated via the deserializeView
        %  function in the future.  Typically this function will return a cell
        %  array of property names and values, however any MATLAB array type is
        %  allowed.
        
        s.XFactor = obj.XFactor;
        s.YFactor = obj.YFactor;
        
        end
        
        function deserializeView(obj,s)
        %DESERIALIZEVIEW Set saved state data
        %  DESERIALIZEVIEW(OBJ, DATA) sets a copy of the serialized state of this
        %  View.  DATA is a MATLAB array that will have been created by a
        %  previous call to SERIALIZEVIEW on this View object.
        
        obj.XFactor = s.XFactor;
        obj.YFactor = s.YFactor;        
        
        end
    end
    
    methods(Access=private)
        function create(obj)
        %create create graphical components
        g = mvgraph2d(obj.Parent,...
            'grid','on',...
            'box','on',...
            'Callback',@obj.onChangeFactor);
        attachContentHandle(obj,g);        

        ax = obj.Axes;
        
        set(ax,'NextPlot','add',...
            'Box','on',...
            'ButtonDownFcn',@obj.onButtonDown);
        xlabel = get(ax,'XLabel');
        ylabel = get(ax,'YLabel');
        set([xlabel,ylabel],'Interpreter','none');
        % set up empty data and factor settings
        g.data = [];
        g.factors = '';
        % give the main line an appropriate tag etc.
        set(g.line,'Tag','main line');
        
        end
        
        function onNodeUpdated(obj,~,~)
        update(obj)
        end
        
        function onButtonDown(obj,hAxes,~)
        notify(obj,'ButtonDown')
        mv_zoom(hAxes)
        end
        
    end
    
    methods(Abstract,Access=protected)
        update(obj)
    end
     
    
    methods(Access=protected)
        function setUIContextMenu(obj)
        set(obj.Axes,'UIContextMenu',obj.UIContextMenu);
        setUIContextMenu@mbcgui.multiview.View(obj);
        end    
        
        function fx = standardFactor(obj,fx)
        ms = obj.MessageService;
        fx(strfind(fx,' ['):end) = [];
        fx = strrep(fx,varname(ms.Model),'<response>');
        
        inpLabels = InputLabels(ms.Model);
        if any(strcmp(fx,inpLabels))
            %code input
            fx = sprintf('<Input%d>',strcmp(fx,inpLabels));
        end
        
        % remove any brackets and trailing spaces
        
        fx = regexprep(fx,'\(.*?\)','');
        fx = regexprep(fx,'\[.*?\]','');
        fx = strtrim(fx);
        
        end
        
        function pos = matchFactor(obj,fx,factors,Other)
        %matchFactor match factor name 
        
        ms = obj.MessageService;
        fx = strrep(fx,'<response>',varname(ms.Model));
        
        pos = find(strncmpi(fx,factors,length(fx)));
        
        if strncmp(fx,'<Input',6)
            % find Input Number
           InpNum = str2double(fx(7:strfind(fx,'>')-1));
           if isfinite(InpNum) && InpNum<=ms.NumInputs
               % find position of input number
               inpLabels = InputLabels(ms.Model);
               pos = find( strcmp(inpLabels{InpNum},factors) );
           end
        end
        
        if length(pos)>1
            % use the first 
            pos = pos(1);
        end
        if isempty(pos)
            pos = 1;
        end
        
        if Other==pos
            pos = find(setdiff(1:length(factors),Other),1,'first');
        end
        
        end
        
        function onChangeFactor(obj,~,~)
        %onChangeFactor change X or Y factor
        %  may need to redraw test numbers
        
        g = obj.Graph2D;
        f = get(g,'factors');
        fx = f{get(g,'currentxfactor')};
        obj.XFactor = standardFactor(obj,fx);
        fy = f{get(g,'currentyfactor')};
        obj.YFactor = standardFactor(obj,fy);
        end        
        
        
    end
    
    
end