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

    function [ssf,OK] = ValDataWizard(p,Title)
%VALDATAWIZARD
%
% ssf = ValDataWizard(p,Title)

%  Copyright 2006-2015 The MathWorks, Inc. 

NodeData = iGetNodeData(p);
pNodeData = xregpointer(NodeData);

[OK, ssf] = xregwizard(mvf, Title, {@iCard1Create}, pNodeData);

freeptr(pNodeData)

%--------------------------------------------------------------------------
function NodeData= iGetNodeData(p)

ssf = valdata(p.info);

T = mdevtestplan(p.info);
IsOneStage = numstages(T)==1 || isRespFeat(p.info);

if isempty(ssf) || ~isa(ssf,'sweepsetfilter')
    % make default data from testplan data
    dp = DataLinkPtr(T);
    ssf = sweepsetfilter(dp);
end

if p==address(T)
    % factors for testplan validation is all inputs and all responses
    FITDATA= getdata(T,'Y');
    factors= get(FITDATA,'Name');
else
    % factors for node is all inputs and response name
    [X,FITDATA]= getdata(p.info,'FIT');
    ms= modelstage(p.info);
    if ms>0
        X= X(ms);
    end
    factors= {};
    for i=1:length(X)
        factors= [factors; get(X{i},'Name')];
    end
    factors= [factors; get(FITDATA,'Name')];
end

% get list of data objects
MP = project(T);
allPtrs = dataptrs(MP.info);
ok= false(1,length(allPtrs));
nf= length(factors);
for i=1:length(allPtrs)
    ok(i)= size(allPtrs(i).info,2)>= nf;
end
allPtrs= allPtrs(ok);

% make a structure to store node information
NodeData.Pointer = p;
NodeData.ssf = ssf;
NodeData.Factors = factors;
NodeData.IsOneStage = IsOneStage;
NodeData.DataPointers = allPtrs;


%--------------------------------------------------------------------------
function lyt = iCard1Create(fh, iFace, NodeData)
% select data set
if isa(fh, 'xregcontainer')
    lyt = fh;
    layoutUD = get(lyt, 'UserData');
else
    iFace.setWizardSize( [600, 300] );
    set(fh,'Resize','off');
    
    % create layout
    txt=uicontrol('Parent',fh,...
        'Style','text',...
        'HorizontalAlignment','left',...
        'String','Datasets:');
    % the list of data sets
    layoutUD.allSweeps=uicontrol('Parent',fh,...
        'Style','listbox',...
        'Tag','sweepsListbox',...
        'Callback',{@iSelectData,iFace},...
        'BackgroundColor','w');
    % info about selected data set (on right)
    infoFrame = mbcgui.container.layoutpanel(...
        'Parent', fh, ...
        'Visible', 'off', ...
        'BorderType', 'etchedin', ...
        'Title', 'Data information', ...
        'LayoutBorder', [5 5 5 5]);
    layoutUD.sweepsetinfo=uicontrol('Parent',infoFrame,...
        'Style','text',...
        'HorizontalAlignment','left');
    set(infoFrame, 'LayoutComponent', layoutUD.sweepsetinfo);

    % checks to choose the data for fitting
    lyt = xreggridbaglayout(fh,...
        'packstatus', 'off', ...
        'border',[20 10 20 10],...
        'gapx', 10, ...
        'dimension',[2,2],...
        'rowsizes',[15,-1],...
        'elements',{txt,layoutUD.allSweeps, [],infoFrame},...
        'correctalg','on',...
        'colsizes',[-1, -1], ...
        'colratios', [2 5]);

    layoutUD.NodeData = NodeData;
    layoutUD.nextFcn = @iCard1Next;
    layoutUD.finishFcn = '';
end



feval(iFace.setFinishButton, 0);
feval(iFace.setNextButton, 1);

layoutUD = iCard1Initialise( layoutUD );
set(lyt, 'UserData', layoutUD);

%--------------------------------------------------------------------------
function ud = iCard1Initialise(ud)

NodeData= ud.NodeData.info;

% set up string in sweepset list box and info field
dp = NodeData.DataPointers;
str=cell(1,length(dp));
for n=1:length(dp);
   str{n}=dp(n).get('label');
end
% set current data set to be the one used for modelling
ind= find(dp==dataptr(NodeData.ssf));
set(ud.allSweeps,'String',str,'Value',ind);

% put comments about the current ss in text on right
ss= dp(ind).sweepset;
set(ud.sweepsetinfo,'String',cellstr(char(ss)));

%--------------------------------------------------------------------------
function iSelectData(src,evt, iFace )

ud = feval(iFace.getCardUserdata);
NodeData = ud.NodeData.info;

NewData = get(ud.allSweeps,'Value');

dp=NodeData.DataPointers(NewData);
ssf = sweepsetfilter(dp);


% put comments about the current ss in text on right
ss= dp.sweepset;
set(ud.sweepsetinfo,'String',cellstr(char(ss)));

% save user data for card
NodeData.ssf = ssf;
ud.NodeData.info = NodeData;


%--------------------------------------------------------------------------
function [nextCardID, localData] = iCard1Next(layoutUD, iFace)
% set next wizard card
localData = layoutUD.NodeData;
NodeData = layoutUD.NodeData.info;
ssf = NodeData.ssf;
if isempty(get(ssf,'variables')) && all(ismember(NodeData.Factors,get(ssf,'Name')))
    nextCardID = @iCard3Create;
else
    % only choose signals for unmatched data
    nextCardID = @iCard2Create;
end

%--------------------------------------------------------------------------
function lyt = iCard2Create(fh, iFace, NodeData)
% match signals
if isa(fh, 'xregcontainer')
    lyt = fh;
    layoutUD = get(lyt, 'UserData');
else
    
    layoutUD.List = mbcwidgets.List( 'Parent', fh,...
        'Editable', false, ...
        'Visible','off',...
        'SelectionChangedCallback',{@iSigChooserHandler, iFace},...
        'SelectionMode', 'SingleRow');
    

    layoutUD.List.Peer.setColumnData({'Symbol','Data Name'});
    layoutUD.List.Peer.setColumnWidths([80  80]);
    

    % Make a title for this ListView
    t= uicontrol('Parent',fh,...
        'Style','text',...
        'HorizontalAlignment','left',...
        'String','Input and response signals that require matching:');

    % Put the ListView into a BorderLayout
    listBrdr= xregborderlayout(fh,...
        'Center',layoutUD.List,...
        'innerborder',[0 0 0 40],...
        'North',t,...
        'packstatus','off');

    % now build the Select button
    selbutton= xregGui.iconuicontrol('parent',fh,...
        'imageFile',[xregrespath,'\leftarrow.bmp'],...
        'transparentColor', [255 255 0],...
        'ToolTip','Select Data Signal',...
        'callback',{@iSelectSignalName,iFace});

    layoutUD.selbutton = selbutton;
    % add the selectbutton to a BorderLayout with BIG borders
    selBrdr= xregborderlayout(fh,...
        'Center',selbutton,...
        'border',[0 50 0 50]);

    % listbox with all Names
    layoutUD.namelist=uicontrol('Parent',fh,...
        'Style','listbox',...
        'Tag','Symlist2',...
        'BackgroundColor',[1 1 1],...
        'Callback',{@iListSelectName,iFace});
    % Make a title for this ListBox
    t= uicontrol('Parent',fh,...
        'Style','text',...
        'HorizontalAlignment','left',...
        'String','All data signals:');

    % add the listbox to a BorderLayout
    listboxBrdr= xregborderlayout(fh,...
        'center',layoutUD.namelist,...
        'innerborder',[0 0 0 40],...
        'north',t);

    % Now put the ListView, Select Button and ListBox into a grid
    lyt = xreggridlayout(fh,...
        'border',[20 10 20 10],...
        'dimension',[1 3],...
        'elements',{listBrdr,selBrdr,listboxBrdr},...
        'gapx',10,...
        'colratios',[0.5 0.15 0.35]);

    layoutUD.NodeData = NodeData;
    layoutUD.nextFcn = @iCard2Next;
    layoutUD.finishFcn = '';
end



feval(iFace.setFinishButton, 0);

layoutUD = iCard2Initialise( layoutUD );
feval(iFace.setNextButton, length(layoutUD.MatchedNames)==length(unique(layoutUD.MatchedNames)));
set(lyt, 'UserData', layoutUD);


%--------------------------------------------------------------------------
function ud = iCard2Initialise( ud )

NodeData = ud.NodeData.info;

ssf = NodeData.ssf;
Factors =NodeData.Factors;

dp= dataptr(ssf);
allNames= get(dp.info,'Name');

Unmatched = ~ismember(Factors,allNames);
Factors= Factors(Unmatched);
ud.UnmatchedNames= Factors;

MatchInd = mbcMatchNames(Factors, allNames,'MatchLevel',0);
MatchedNames = allNames(MatchInd);
% find user variables
vars= get(ssf,'variables');
for i=1:length(vars)
    MatchedFactor= strcmp(  vars(i).varName,Factors );
    MatchedNames{ MatchedFactor } =  vars(i).varExp;
end
ud.MatchedNames = MatchedNames;

ud.List.Peer.setData([Factors(:),MatchedNames(:)])
ud.List.selectRows(1);

ind= find(strcmp(MatchedNames{1},allNames));
set(ud.namelist,'String',allNames,'Value',ind);


%--------------------------------------------------------------------------
function iSigChooserHandler(h, evt, iFace)
% Activex event handler for listbox
% Click and KeyUp events

% KeyUp events have extra data (keycodes etc) so varargin has 5 elements,
% whereas for Click it only has 3 elements.  In both cases the last element
% is the event type, and the last but one contains the event data.



ud = feval(iFace.getCardUserdata);
ind = ud.List.getSelectedRows;
if ~isempty(ind)
    VarIndex= find(strcmp(ud.MatchedNames(ind),get(ud.namelist,'String')));
    set(ud.namelist,'Value',VarIndex);
    set(ud.selbutton,'Enable','on')
else
    set(ud.selbutton,'Enable','off')
end


%--------------------------------------------------------------------------
function iListSelectName(src,null,iFace)
mode=get(gcbf,'SelectionType');
if strcmp(mode,'open');
    iSelectSignalName([],[],iFace);
end

%--------------------------------------------------------------------------
function iSelectSignalName(src, null,iFace)
ud = feval(iFace.getCardUserdata);

% get handles
target=ud.List.getSelectedRows;
curlst=ud.namelist;

% get the variable name to change
AllNames=get(curlst,'String');
curName=AllNames{get(curlst,'Value')};

ud.List.Peer.setDataAt(curName,target-1,1);

% Get the number of nodes
N= ud.List.getRowCount;
% Get the current index
ud.MatchedNames{target} = curName;
% set userdata
feval(iFace.setCardUserdata,ud);

% enable next button if all names are unique
EnableNext = length(ud.MatchedNames)==length(unique(ud.MatchedNames));
feval(iFace.setNextButton,EnableNext);

% select next item
newInd= mod(double(target)+1,double(N)+1);
% make sure indexing starts from 1 not 0
if newInd==0,newInd=1;end
% increment the selected item by one

ud.List.selectRows(newInd);
% select matched name in variable list
VarIndex= find(strcmp(ud.MatchedNames(newInd),get(ud.namelist,'String')));
set(ud.namelist,'Value',VarIndex);

%--------------------------------------------------------------------------
function [nextCardID, localData] = iCard2Next(layoutUD, iFace)
% set next wizard card
localData = layoutUD.NodeData;

NodeData= localData.info;

ssf = NodeData.ssf;
if numVariables(ssf)>0
    ssf= removeVariable(ssf,1:numVariables(ssf));
end

for i =  1:length(layoutUD.UnmatchedNames)
    ssf = addVariable(ssf,[layoutUD.UnmatchedNames{i} '=' layoutUD.MatchedNames{i}]);
end
NodeData.ssf = ssf;
localData.info = NodeData;
nextCardID = @iCard3Create;

%--------------------------------------------------------------------------
function lyt = iCard3Create(fh, iFace, NodeData)
if isa(fh, 'xregcontainer')
    lyt = fh;
    layoutUD = get(lyt, 'UserData');
else
    % sweep chooser twin listbox thing
    layoutUD.ls=listitemselector(fh,'selectionstyle','multiple',...
        'selectedtitle','Selected tests:',...
        'unselectedtitle','Unselected tests:', ...
        'Visible', 'off');
    
    testFrame = mbcgui.container.layoutpanel(...
        'Parent', fh, ...
        'Visible', 'off', ...
        'BorderType', 'etchedin', ...
        'Title', 'Test information');
    layoutUD.sweepnum=uicontrol('Parent',testFrame,...
        'Style','text',...
        'HorizontalAlignment','left',...
        'String','Test number:');
    layoutUD.sweepinfo = mbcgui.table.InfoPane(...
        'Parent', testFrame, ...
        'TitleText', {'Variable', 'Value'}, ...
        'BorderType', 'beveledin');
    
    grd1=xreggridbaglayout(testFrame, ...
        'packstatus', 'off', ...
        'dimension',[2 1],...
        'rowsizes',[20 -1],...
        'border', [5 5 5 5], ...
        'elements',{layoutUD.sweepnum,layoutUD.sweepinfo});
    set(testFrame, 'LayoutComponent', {grd1});

    lyt = xreggridbaglayout(fh, ....
        'border', [20 10 20 10], ...
        'dimension', [1 2], ...
        'colsizes', [-1 200], ...
        'gapx', 10, ...
        'elements', {layoutUD.ls, testFrame});
    
    set(layoutUD.ls,'Callback',{@iRefilter,iFace});
    set(layoutUD.ls,'selectfcn',{@iSweepSelect,iFace});
    
    layoutUD.NodeData = NodeData;
    layoutUD.nextFcn = '';
    layoutUD.finishFcn = @iFinish;
end

feval(iFace.setFinishButton, 1);
feval(iFace.setNextButton, 0);

layoutUD = iCard3Initialise( layoutUD );
set(lyt, 'UserData', layoutUD);

%--------------------------------------------------------------------------
function ud = iCard3Initialise(ud)

NodeData = ud.NodeData.info;
ssf =NodeData.ssf;
% select only required signals
ssf = setVarsFilter(ssf,NodeData.Factors);

dp = dataptr(ssf);
tn= testnum(ssf);
tnAll= dp.testnum;
[OK,items] = ismember(tn,tnAll);
% clear sweep selection
ssf= clearReorderSweeps(ssf);
NodeData.ssf = ssf;
ud.NodeData.info = NodeData;

set(ud.ls,'itemlist',testnum(ssf));
% select all by default
selectitems(ud.ls,items);


% put var names into ActiveX thingy: no values displayed yet
varn=get(ssf,'Name');
ud.sweepinfo.ListText = [varn(:),cell(numel(varn), 1)];

set(ud.sweepnum,'String','Test number:');
if ~isempty(items)
   iSweepinfo(ud,items(1));
end

%--------------------------------------------------------------------------
function iRefilter(src,null,iFace)

ud= feval(iFace.getCardUserdata);

ss = ud.NodeData.info.ssf;

% get current selected test numbers
old_selind= get(ud.ls,'selecteditems');
new_selind = find(ismember(testnum(ss),old_selind));

% select all possible testnums from the current sweepset matching old selindices
% do this in the listitemselector
set(ud.ls,'itemlist',testnum(ss));
selectitems(ud.ls,new_selind);

iSweepinfo(ud,1);

%--------------------------------------------------------------------------
function iSweepSelect(src,evt,iFace)

ud= feval(iFace.getCardUserdata);
iSweepinfo(ud,evt.Index)

%--------------------------------------------------------------------------
function iSweepinfo(ud,inds)
% click on a  sweep and see its values

NodeData = ud.NodeData.info;

listdata = ud.sweepinfo.ListText;
if length(inds)==1
    ss= sweepset(NodeData.ssf(:,:,inds));
    mn=mean(ss);
    listdata(:,2) = arrayfun(@num2str, mn(:), 'UniformOutput', false);
    ud.sweepinfo.ListText = listdata;
    set(ud.sweepnum,'String',['Test number:  ' sprintf('%3g',testnum(ss))]);
else
    % clear out values
    listdata(:,2) = {''};
    ud.sweepinfo.ListText = listdata;
    set(ud.sweepnum,'String','Test number:');
end

%--------------------------------------------------------------------------
function iFinish(layoutUD, iFace)

NodeData= layoutUD.NodeData.info;
ssf = NodeData.ssf;

Items = get(layoutUD.ls,'Selectedindices');
ssf = reorderSweeps(ssf,Items);

if NodeData.IsOneStage && size(ssf,1)~=size(ssf,3)
    % make data one-stage
%    ssf= modifyTestDefinition(ssf,{'rec#',0.5,false,''});
end

feval(iFace.setOutputData, ssf);