www.gusucode.com > mbcview 工具箱matlab源码程序 > mbcview/@cgdatasetnode/ImportTable.m

    function varargout = ImportTable(nd,varargin)
%IMPORTTABLE

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


action = varargin{1};

switch lower(action)
    case 'create'
        varargout{1} = i_Create(varargin{2:end});
    case 'return_data'
        varargout = i_ReturnData(nd);
    case 'close'
        i_Close;
    case 'ok'
        i_OK;
end


function out = i_Create(varargin)

%The inputs on creation are...
% h - call handle
% opt - 'create_new', 'fill_existing'
% oppoint - current oppoint

h_this = findobj(0 , 'Tag' , 'importtable');
if ~isempty(h_this)
    delete(h_this);
end

if nargin<1
    Handles.Daddy = [];
    cage_pos = [100 300 300 400];
else
    Handles.Daddy = varargin{1};
    cage_pos = get(Handles.Daddy , 'Position');
end
if nargin<2
    opt = 'create_new';
else
    opt = varargin{2};
end
if nargin<3
    d.oppoint = [];
else
    d.oppoint = varargin{3};
end

d.fillopt = 0;
if isempty(d.oppoint)   %cannot fill, so create new.
    opt = 'create_new';
end
switch lower(opt)
    case 'fill_existing'
        titlestr = 'Fill data set from table:';
        d.fillopt = 1;
    case 'create_new'
        titlestr = 'Overwrite data set from table:';
    otherwise
        error(message('mbc:cgdatasetnode:InvalidArgument'));
end

% Create list of tables to use for filling data set
c = cgbrowser;
pr = c.RootNode;
nds = pr.preorder('getnode');
tableList = [];
tableListStr = [];
included = [];
done = [];
tpfilter = cgtypes.cgtabletype;
for i = 1:length(nds)
    if matchtype(typeobject(nds{i}),tpfilter)
        ptr = getdata(nds{i});
        if ~ptr.isa('cgnormaliser') && ...
                (isempty(done) || ~ismember(double(ptr),done))
            done = [done double(ptr)];
            if ~d.fillopt || i_check_eval(d.oppoint,ptr)
                include = (~d.fillopt | isfactor(d.oppoint,ptr));
                if include && d.fillopt
                    addstr = '* ';
                else
                    addstr = '  ';
                end
                tableList = [tableList;ptr];  %list of ptrs to available tables
                tableListStr = [tableListStr,{[addstr ptr.getname]}];
                included = [included include];
            end
        end
    end
end

if isempty(tableList)
    thiserrstr = 'The columns in this data set do not completely match the axes of any of the tables in the session. Cannot fill this data set from a table';
    out = errordlg(thiserrstr, 'Import Data from Table', 'modal');
    return;
end
d.included = included;

figW = 350;
figH = 175;
pos = [cage_pos(1)+100 cage_pos(2)+cage_pos(4)-140-200 figW figH];

SC = xregGui.SystemColorsDbl;
Handles.Figure = xregfigure('Position' , pos , ...
    'Resize' , 'off' , ...
    'Name' , 'Import Data from Table' , ...
    'CloseRequestFcn',@i_Close,...
    'Tag' , 'importtable',...
    'WindowStyle' , 'modal');
out = Handles.Figure;

uicontrol('Style' , 'text' , ...
    'Parent' , Handles.Figure , ...
    'HorizontalAlignment' , 'left' , ...
    'Position' , [10 figH-25 figW-20 15] , ...
    'String' , titlestr);
Handles.TableList = uicontrol('Style' , 'popupmenu' , ...
    'Parent' , Handles.Figure , ...
    'HorizontalAlignment' , 'left' , ...
    'BackgroundColor' , SC.WINDOW_BG , ...
    'Position' , [10 figH-45 figW-20 20] , ...
    'String' , tableListStr,...
    'UserData',tableList,...
    'Callback',@i_Select);
Handles.TableCheck = uicontrol('Style' , 'checkbox' , ...
    'Parent' , Handles.Figure , ...
    'Position' , [10 figH-75 figW-20 20] , ...
    'Value',0,...
    'String','Include table output');
Handles.AddString = uicontrol('Style','text',...
    'Parent' , Handles.Figure , ...
    'Position' , [25 figH-90 figW-35 15] ,...
    'String', '(Add as new factor)',...
    'HorizontalAlignment','left',...
    'Visible','off');
Handles.TableInfo = uicontrol('Style' , 'edit' , ...
    'Parent' , Handles.Figure , ...
    'HorizontalAlignment','left',...
    'Tag','tablecheck',...
    'Enable', 'inactive');
xregGui.labelcontrol('parent', Handles.Figure, ...
    'String', 'Table description:', ...
    'LabelSizeMode', 'absolute', ...
    'ControlSizeMode', 'relative', ...
    'LabelSize', 100, ...
    'ControlSize', 1, ...
    'Control', Handles.TableInfo, ...
    'Position', [10 figH-120 figW-20 20]);

uicontrol('Style' , 'push' , ...
    'Parent' , Handles.Figure , ...
    'Position' , [279 7 65 25] , ...
    'String' , 'Cancel' , ...
    'Callback' , @i_Close);
uicontrol('Style' , 'push' , ...
    'Parent' , Handles.Figure , ...
    'Position' , [207 7 65 25] , ...
    'String' , 'OK' , ...
    'Callback' , @i_OK);

d.TableList = tableList;
d.Handles = Handles;
set(Handles.Figure , 'UserData' , d);
i_Select;

%-------------------------------------------------------------------
function valid = i_check_eval(oppoint,tptr)
%-------------------------------------------------------------------
valid = 1;
t_ptrs = get(tptr.info,'axesptrs');
ptrlist = oppoint.ptrlist;
for i = 1:length(t_ptrs)
    if ~any(t_ptrs(i)==ptrlist)
        valid = 0;
    end
end

%-------------------------------------------------------------------
function i_Select(varargin)
%-------------------------------------------------------------------
d = i_GetData;
ind = get(d.Handles.TableList,'Value');
if isempty(ind)
    ind = 1;
end
set(d.Handles.TableList,'Value',ind);

check = 0; 
str = '';
set(d.Handles.AddString,'Visible','off');
if isempty(d.TableList)
    d.Table = [];
    str = '';
else
    d.Table = d.TableList(ind);
    switch lower(class(d.Table.info))
        case 'cglookuptwo'
            type = '2-D Table';
        case 'cglookupone'
            type = '1-D Table';
        case 'cgnormfunction'
            type = 'Function';
        otherwise
            type = '';
    end
    if ~isempty(type)
        t_ptrs = d.Table.get('axesptrs');
        prettyaxesnames = [];
        for i = 1:length(t_ptrs)
            prettyaxesnames = [prettyaxesnames t_ptrs(i).getname ', '];
        end
        prettyaxesnames = prettyaxesnames(1:end-2);
        values = get(d.Table.info,'values');
        if isempty(values)
            str =[type,' object with no values'];
        else
            [m,n]=size(values);
            if n==1
                str = [type,' (',prettyaxesnames,') with ' num2str(m) ' breakpoints'];
            else
                str = [type,' (',prettyaxesnames,') : Size ' num2str(m) ' by ' num2str(n)];
            end
        end
        if ~d.included(ind) && d.fillopt
            set(d.Handles.AddString,'Visible','on','String','(Add new factor)');
        elseif d.fillopt
            set(d.Handles.AddString,'Visible','on','String','(Factor already present)');
        end
        d.tableincluded = d.included(ind);
        check = ~d.fillopt | d.tableincluded;
        d.axesstring = prettyaxesnames;
    end
end
set(d.Handles.TableInfo,'String',str);
set(d.Handles.Figure , 'UserData' , d);
set(d.Handles.TableCheck,'Value',check);

%-------------------------------------------------------------------
function i_Close(varargin)
%-------------------------------------------------------------------
d=i_GetData;
if ~isempty(d)
    delete(d.Handles.Figure);
end

%-------------------------------------------------------------------
function argout = i_ReturnData(nd)
%-------------------------------------------------------------------
d = i_GetData;
if isempty(d) || isempty(d.Table) || ~isvalid(d.Table)
    argout{1} = 0;
    argout{2} = [];
else
    check = get(d.Handles.TableCheck,'Value');
    ok = 1;
    [op,mess] = ImportTable(cgoppoint,d.Table,check);
    if ~isempty(mess)
        h = errordlg(mess, 'Cage' , 'modal');
        uiwait(h);
        ok = 0;
    else
        % Ensure new name is unique in project, except for this node
        ndname = name(nd);
        newname = getname(op);
        if ~strcmp(newname, ndname)
            nd = name(nd, '__tmpname');
            P = project(nd);
            op = setname(op, uniquename(P, newname));
            nd = name(nd, ndname);
        end
    end
    argout{1} = ok;
    argout{2} = op;
    if ~isempty(d.Handles.Daddy)
        if strcmp(get(d.Handles.Daddy,'Visible'),'on')
            figure(d.Handles.Daddy);
        end
    end
end


%-------------------------------------------------------------------
function i_OK(varargin)
%-------------------------------------------------------------------
d = i_GetData;
uiresume(d.Handles.Figure);


%-------------------------------------------------------------------
function d = i_GetData
%-------------------------------------------------------------------
h_this = findobj(0 , 'Tag' , 'importtable');
if isempty(h_this)
    d = [];
    return
end
d = get(h_this , 'UserData');