www.gusucode.com > mbcdata 工具箱 matlab 源码程序 > mbcdata/@cgoptim/guiInitFromTableGrid.m

    function [optim, OK] = guiInitFromTableGrid(optim, pPROJ)
%GUIINITFROMTABLEGRID Initialise fixed and free variables from a table grid.
%
%   [OPTIM, OK] = GUIINITFROMTABLEGRID(OPTIM, PPROJ) displays a dialog that
%   lets the user pull in initial fixed and free values from a table grid.
%   They also have the opportunity to alter the number of runs in the
%   optimization while doing this.  
%
%   See also CGOPTIM/GUIINITFROMDATASET, CGOPTIM/GUIINITFROMOUTPUT,
%   CGOPTIM/GUIINITFROMTABLEVALUES

%   Copyright 2006-2015 The MathWorks, Inc.

% Create the dialog
figSize = [395 300];
ud.hDlg = mbcgui.container.Dialog('Name', 'Import From Table Grid', ...
    'Tag', 'OptimTableGridImport', ...
    'Buttons', 'OK_CANCEL',...
    'Size', figSize, ...
    'PersistPosition', true);
hFig = ud.hDlg.Figure;

% Create a run time pointer to store useful info
hInfo = xregGui.RunTimePointer;

% Create the table
txt = uicontrol('Parent', hFig, ...
    'Style', 'text', ...
    'HorizontalAlignment', 'left', ...
    'String', 'Select table to import grid from:');
ud.hTab = mbcwidgets.Table1D('list', ...
    'Parent', hFig, ...
    'SelectionMode', 'SingleRow', ...   
    'Enable', 'on', ...
    'SelectionChangedCallback', {@i_tableChange, hInfo}, ...
    'Visible', 'on');

% Set the columns
props = {'Name', 'Size', 'Row Input', 'Column Input'};
ud.hTab.Peer.setColumnData(props);
ud.hTab.Peer.setColumnWidths([150, 60, 75, 75]);

% Set data in table
[info, icons, ud.pTab] = i_getTableInformation(optim, pPROJ);
ud.hTab.Peer.setIconBaseLocation(cgrespath);
ud.hTab.Peer.setData( info, icons );

% Import Options layout
hImportOptionPanel = mbcgui.container.layoutpanel('Parent', hFig, ...
    'Title', 'Import options', ...
    'BorderType', 'etchedin', ...
    'LayoutBorder', [10 10 10 5]);
ud.ImportOption = xregGui.rbgroup('parent', hImportOptionPanel, ...
    'nx', 1, ...
    'ny', 2, ...
    'String', {'Use one table cell for each run (alter number of runs)'; ...
    'Use all table cells for each run (alter length of each variable)'}, ...    
    'Callback', '', ...
    'Tag', 'rbgroupImportOptions');
set(hImportOptionPanel, 'LayoutComponent', ud.ImportOption);

% Create layout
ud.hDlg.Content = xreggridbaglayout(hFig, ...
    'dimension', [6 1], ...
    'rowsizes', [2 15 2 -1 15 70], ...
    'elements', {[], txt, [], ud.hTab, [], hImportOptionPanel});

% Save user data
ud.OptimNumRuns = getNumRuns(optim);
hInfo.info = ud;

% Launch dialog
i_enableOK(hInfo);
closeMode = ud.hDlg.showDialog();

% Get the return
OK = strcmp(closeMode, 'OK');
if OK
   
    % Get selected table
    pTab = ud.pTab(ud.hTab.getSelectedRows);
        
    % Set table data in optimization
    if ud.ImportOption.Selected == 2
        % Alter number of variables to accommodate table data       
        optim = setinitialvaluedatafromtablegrid(optim, pTab, true);
    else
        % Alter number of runs to accommodate table data
        nRunsTable = prod(getTableSize(pTab.info));
        optim = setNumRuns(optim, nRunsTable);        
        optim = setinitialvaluedatafromtablegrid(optim, pTab, false);
    end

end

% Delete and exit
ud.hDlg.delete;

%--------------------------------------------------------------------------
function [info, icons, pTab] = i_getTableInformation(optim, pPROJ)
%--------------------------------------------------------------------------

% Get tables and normalizers. Restrict the tables that can be used to those
% that have a single input per axis. Further restriction: all table inputs
% must be in the optimization fixed/free variables
conn = pPROJ.UpdateConnections;
pTab = conn.pData(find(conn, 'Type', 'Table'));
tabIcons = pveceval(pTab, @iconfile);
pNorm = conn.pData(find(conn, 'Type', 'Normalizer'));
normIcons = pveceval(pNorm, @iconfile);
pTab = [pTab, pNorm];
icons = [tabIcons, normIcons];
pOptimInps = [getfreevalues(optim), getfixedvalues(optim)];
idx = parrayeval(pTab, @i_isValidTable, {pOptimInps}, mbclogical);
pTab = pTab(idx); 
icons = icons(idx);

% Get information
tabNames = pveceval(pTab, @getname);
tabSizes = pveceval(pTab, @getTableSize);
tabSizes = cellfun(@i_tabSizeToString, tabSizes, 'UniformOutput', false);
pTabInps  = pveceval(pTab, @getinports);
nTab = length(pTab);
tabInpNames = cell(nTab, 2);
for i = 1:nTab
    tabInpNames(i, :) = i_getTabInpNames(pTabInps{i});
end
info = [tabNames', tabSizes', tabInpNames];

%--------------------------------------------------------------------------
function names = i_getTabInpNames(pInp)
%--------------------------------------------------------------------------

names = cell(1, 2);
names(1:length(pInp)) = pveceval(pInp, @getname);

%--------------------------------------------------------------------------
function str = i_tabSizeToString(vec)
%--------------------------------------------------------------------------

if numel(vec) == 1
    vec(2) = 1;
end
str = sprintf('%dx%d', vec(1), vec(2));

%--------------------------------------------------------------------------
function stat = i_isValidTable(table, pOptimInps)
%--------------------------------------------------------------------------

pTableInps = getinports(table);
stat = prod(getTableSize(table))>0 && issimpletable(table) ...
    && ~any(cgisindependentvars(pTableInps, pOptimInps));

%--------------------------------------------------------------------------
function i_tableChange(src, evt, hInfo)
%--------------------------------------------------------------------------

% Set enabled state of OK button
i_enableOK(hInfo);

%--------------------------------------------------------------------------
function i_enableOK(hInfo)
%--------------------------------------------------------------------------

ud = hInfo.info;

% Set Enable state
if isempty(ud.hTab.getSelectedRows)
    ud.hDlg.disableButtons('OK');
else
    ud.hDlg.enableButtons('OK');
end