www.gusucode.com > mbcdesign 工具箱 matlab 源码程序 > mbcdesign/matchInputsToCageTable.m

    function [matches, tableIndex, OK] = matchInputsToCageTable(designFactors, type)
%matchDesignToCageTable 

%  Copyright 2015 The MathWorks, Inc. 

dlg = mbcgui.container.Dialog( ...
    'Name', 'Select Table', ...
    'Size',[750 350],...
    'Resize','off' );

dlg.Content = createLayout(dlg.Figure,designFactors,type);

dlg.ValidationFcn = @()validate(dlg.Content);

closeAction = dlg.showDialog();
if strcmp(closeAction,'OK')
    % get results
    [tableIndex, matches] = finalise(dlg.Content);
    OK = true;
else
    % user clicked cancel, return empty output
    tableIndex = []; 
    matches = [];
    OK = false;
end
delete(dlg);


function matchingOK = validate(layout)
%validate validate current dialog state

ud = get(layout, 'UserData');
matchingOK = mbcMatchDialog('validate', ud.matchLayout);


function [tableIndex, matches] = finalise(layout)
%finalise get results from current state of dialog

ud = get(layout, 'UserData');
matches = mbcMatchDialog('finalise', ud.matchLayout);
tableIndex = ud.mapping(ud.tableList.SelectedIndex, :);


function layout = createLayout(parent, designFactors, type)
%createLayout Create dialog layout

% setup table list
[~, tablePointers, mapping] = i_findSimpleTableNames(type);
ud.tableList = cgtools.exprList('Parent', parent,...
    'DisplayInputNames', 'inports', ...
    'ItemHeaderText','CAGE tables', ...
    'DisplayTypeColumn', false, ...
    'Items', tablePointers);
% set the initial selected row
ud.tableList.SelectedIndex = 1;

ud.parent = parent;
ud.designFactors = designFactors;
ud.type = type;
ud.mapping = mapping;

% create input matching layout
cif = iConInputFactorFor(ud, ud.tableList.SelectedIndex);
ud.matchLayout = mbcMatchDialog('layout', parent, cif, designFactors, ...
    'Match Tables Inputs and Outputs', 'Design Inputs');

% table selection callback
ud.clickList = handle.listener(ud.tableList, 'SelectionChanged', {@iSelectTable, ud});

% arrange components in a layout
layout = xreggridbaglayout(parent, ...
    'dimension', [1 2], ...
    'colsizes', [180 -1], ...
    'gapx',10,...
    'border', [7, 0, 0, 7], ...
    'elements', {ud.tableList, ud.matchLayout});

set(layout, 'UserData', ud);


function iSelectTable(src, ~, ud)
%iSelectTable Table selection callback
cif = iConInputFactorFor(ud, src.SelectedIndex);
% update matching layout with new input factors for selected table
mbcMatchDialog('initialise', ud.matchLayout, cif, ud.designFactors);


function cif = iConInputFactorFor(ud, selectedRow)
%iConInputFactorFor constructs conInputFactor for a CAGE table
index = ud.mapping(selectedRow,:);

[tableName, inputNames, breakPoints, values] = i_getTableInfo(ud.type, index);
switch ud.type
    case '2d'
        xName = inputNames{2};
        yName = inputNames{1};
        cif = coninputfactor(...
            {[xName,'(X Factor)'], [yName,'(Y Factor)'], [tableName,'(Z Factor)']}, ...
            {xName, yName, tableName}, ...
            {'','',''}, ...
            [min(breakPoints{2}), min(breakPoints{1}), min(values(:))], ...
            [max(breakPoints{2}), max(breakPoints{1}), max(values(:))]);
    case '1d'
        xName = inputNames{1};
        cif = coninputfactor(...
            {[xName,'(X Factor)'], [tableName,'(Y Factor)']}, ...
            {xName, tableName}, ...
            {'',''}, ...
            [min(breakPoints{1}), min(values(:))], ...
            [max(breakPoints{1}), max(values(:))]);
end


function [tableName, inputNames, breakPoints, values] = i_getTableInfo(type, index)
%i_getTableInfo Get names and values from CAGE table
tableProvider = mbcutils.GenericTableProvider.Instance.getTableProvider(index(1));
tableIndex = index(2);
[tableName, inputNames] = getTableFactorNames(tableProvider, type, tableIndex);
[breakPoints, values] = getTableValues(tableProvider, type, tableIndex);


function [names, pointers, tableIndexMapping] = i_findSimpleTableNames(type)
%i_findSimpleTableNames Find tables of correct type that can be imported
names = {};
tableIndexMapping = [];
pointers = [];
tableProviders = mbcutils.GenericTableProvider.Instance.RegisteredTableProviders;
for i=1:length(tableProviders)
    [ithNames,ithPointers] = tableProviders{i}.getTableList(type);
    names = [names; ithNames];
    numNames = length(ithNames);
    tableIndexMapping = [tableIndexMapping; [i*ones(numNames,1), (1:numNames)']];
    pointers = [pointers, ithPointers];
end