www.gusucode.com > mbcdata 工具箱 matlab 源码程序 > mbcdata/@cgoptim/guiInitFromTableValues.m
function [optim, OK] = guiInitFromTableValues(optim, pPROJ) %GUIINITFROMTABLEVALUES Initialise fixed and free variables from table values. % % [OPTIM, OK] = GUIINITFROMTABLEVALUES(OPTIM, PPROJ) displays a dialog % that lets the user pull in initial fixed and free values from the % evaluation of tables. Table inputs must be in the current set of % optimization fixed/free variables. They also have the opportunity to % alter the number of runs in the optimization while doing this. % % See also CGOPTIM/GUIINITFROMDATASET, CGOPTIM/GUIINITFROMOUTPUT, % CGOPTIM/GUIINITFROMTABLEGRID % Copyright 2006-2015 The MathWorks, Inc. figTitle = 'Import From Table Values'; % Create run time pointer to hold user data hInfo = xregGui.RunTimePointer; % Optimization fixed/free variables ud.OptimValuePointers = [getfreevalues(optim), getfixedvalues(optim)]; ud.OptimValueNames = pveceval(ud.OptimValuePointers, @getname); % Store the matching indices and boolean selected index. Cannot rely on the % java thread updating in time. ud.MatchInd = zeros(1, length(ud.OptimValueNames)); ud.ItemIncluded = false(1, length(ud.OptimValueNames)); % All inputs must be of the same length or scalar. Show an error dialog and % leave if this is not the case. tabInpVal = getinitialvaluedata(optim, ud.OptimValuePointers); lenInpVal = cellfun('size', tabInpVal, 2); maxLen = max(lenInpVal); if ~all(lenInpVal == 1 | lenInpVal == maxLen) errStr = sprintf('%s\n\n%s', 'Cannot import values from tables.', ... ['All optimization fixed and free variables must have a ', ... 'common length or be scalar']); h = errordlg(errStr, figTitle, 'modal'); waitfor(h); OK = false; return end % Create figure hFig = xregdialog('Name', figTitle); xregcenterfigure(hFig, [400 300]); % Create main controls hOptimLabel = uicontrol('Parent', hFig, ... 'Style', 'text', ... 'String', 'Select table values to import:', ... 'HorizontalAlignment', 'left'); P = com.mathworks.toolbox.mbc.gui.peer.OptimDataImportTablePeer; ud.hMatchTable = mbcwidgets.Table1D(P, ... 'Parent', hFig, ... 'ValueChangedCallback', {@i_tableValueChanged, hInfo}, ... 'Editable', true); % OK/Cancel dialog controls hOK = uicontrol('Parent', hFig, ... 'String', 'OK', ... 'Style', 'pushbutton', ... 'Callback', @(src, evt) set(hFig, 'Tag', 'ok', 'Visible', 'off')); hCancel = uicontrol('Parent', hFig, ... 'String', 'Cancel', ... 'Style', 'pushbutton', ... 'Callback', @(src, evt) set(hFig, 'Visible', 'off')); OKCclLyt = xreggridbaglayout(hFig, ... 'dimension', [1 3], ... 'colsizes', [-1 65 65], ... 'gapx', 7, ... 'elements', {[], hOK, hCancel}); % Layout MainLyt = xreggridbaglayout(hFig, ... 'dimension', [5 3], ... 'rowsizes', [15 2 -1 10 25], ... 'colsizes', [150 150 -1], ... 'border', [7 7 7 7], ... 'mergeblock', {[1 1] [1 3]}, ... 'mergeblock', {[3 3] [1 3]}, ... 'mergeblock', {[5 5] [1 3]}, ... 'elements', {hOptimLabel, [], ud.hMatchTable, [], OKCclLyt, ... [], [], [], [], [], ... [], [], [], [], [] }); infoStr = ['Fill optimization input variable values with the evaluation ', ... 'of a table. Unmatched inputs are unaltered by the import. Matched ', ... 'inputs are replaced with an evaluation of the table at the current ', ... 'optimization values.']; hInfoPane = mbcgui.container.InfoPane('Parent', hFig, ... 'InfoHeight', 45, ... 'InfoString', infoStr, ... 'Center', MainLyt); MainLyt = xreglayerlayout(hFig, 'Elements', {hInfoPane}); hFig.LayoutManager = MainLyt; % Initialisation. Store the potential table inputs. At this point, all the % optim fixed/free inputs are table inputs. Store potential tables. ud.pPROJ = pPROJ; ud.pTableInps = mbcpointer(0, 0); i_initInputList(ud); ud.pTabs = i_findTables(ud); [ud.pTabs, ud.MatchInd] = i_resetTableList(ud); % Set user data hInfo.info = ud; % Launch dialog hFig.showDialog(hOK); % Perform actions tg = hFig.Tag; OK = strcmp(tg, 'ok'); if OK % Get user data ud = hInfo.info; % Inputs to import table evaluations into MatchInd = double(iFindItemMatch(ud.hMatchTable)).'; DoImport = iFindItemIncluded(ud.hMatchTable).' & MatchInd > 0; pInp = ud.OptimValuePointers(DoImport); % Tables that need evaluating pTab = ud.pTabs(MatchInd(DoImport)); % Set initial value optim = setinitialvaluedatafromtablevalues(optim, pInp, pTab); end % Delete and leave delete(hFig); function matchInds = iFindItemMatch(table) matchInds = javaMethodEDT('getItemDataMatchInd', table.Peer)+1; function matchInds = iFindItemIncluded(table) matchInds = javaMethodEDT('getItemIncluded', table.Peer); %%%%%%%%%%%%% % Callbacks % %%%%%%%%%%%%% %-------------------------------------------------------------------------- function i_tableValueChanged(src, evt, hInfo) %-------------------------------------------------------------------------- % Get user data ud = hInfo.info; % Check to see if we've selected a table "input". If so then set it back to % empty and return rowSel = evt.data.Rows; OutDataInd = src.Peer.ItemDataMatchInd; thisOutDataInd = OutDataInd(rowSel) + 1; isIndependent = true; if thisOutDataInd % Table (i.e. not empty) selected. pOptimValueSel = ud.OptimValuePointers(rowSel); pThisTableInps = getinports(ud.pTabs(thisOutDataInd).info); pPossibleNewTableInps = unique([ud.pTableInps, pThisTableInps]); if ~cgisindependentvars(pOptimValueSel, pPossibleNewTableInps) ud.MatchInd(rowSel) = 0; ud.ItemIncluded(rowSel) = false; src.Peer.setItemInformation(ud.OptimValueNames, ... ud.MatchInd-1, ud.ItemIncluded); isIndependent = false; end end if isIndependent % Update the matching indices for the Java object if evt.Data.Columns == 1 ud.ItemIncluded(evt.Data.Rows) = evt.Data.NewValue; elseif evt.Data.Columns == 3 % Shift selection to be 0 and not -1 based. That is, 0 is the first % blank selection in the pop down menus. NewSel = evt.Data.NewValue + 1; ud.MatchInd(rowSel) = NewSel; ud.ItemIncluded(rowSel) = (NewSel>0); else error(message('mbc:cgoptim:InvalidState10')); end % Update list of possible tables [ud.pTabs, ud.MatchInd] = i_resetTableList(ud); % Update the table "input" list. MatchInd = ud.MatchInd(ud.ItemIncluded); pMatchTabs = ud.pTabs(MatchInd(MatchInd>0)); ud.pTableInps = pveceval(pMatchTabs, @getinports); ud.pTableInps = [ud.pTableInps{:}]; end % Set user data hInfo.info = ud; %%%%%%%%%%%%%%%%%%%%% % Utility Functions % %%%%%%%%%%%%%%%%%%%%% %-------------------------------------------------------------------------- function i_initInputList(ud) %-------------------------------------------------------------------------- % Initialise matching nInputs = length(ud.OptimValueNames); OutDataInd = 1:nInputs; OutDataMatched = false(1, nInputs); % Set data into java table ud.hMatchTable.Peer.setStoreSwingCalls(true); ud.hMatchTable.Peer.setColumnName(1, 'Fill Input With'); ud.hMatchTable.Peer.setItemInformation(ud.OptimValueNames, OutDataInd-1, OutDataMatched); ud.hMatchTable.Peer.setStoreSwingCalls(false); %-------------------------------------------------------------------------- function pTabs = i_findTables(ud) %-------------------------------------------------------------------------- % Tables/normalisers must have inputs that do not have a table matched, be % simple tables and be non empty % Potential inputs MatchInd = double(iFindItemMatch(ud.hMatchTable)).'; IsInput = ~iFindItemIncluded(ud.hMatchTable).' | MatchInd==0; pInps = ud.OptimValuePointers(IsInput); % Potential tables conn = UpdateConnections(ud.pPROJ.info); pTabs = conn.pData(find(conn, 'Type', 'Table')); pTabs = [pTabs, conn.pData(find(conn, 'Type', 'Normaliser'))]; idx = parrayeval(pTabs, @i_isValidTable, {pInps}, mbclogical); pTabs = pTabs(idx); %-------------------------------------------------------------------------- function stat = i_isValidTable(table, pInps) %-------------------------------------------------------------------------- pThisInps = getinports(table); stat = prod(getTableSize(table))>0 && issimpletable(table) && ... ~any(cgisindependentvars(pThisInps, pInps)); %-------------------------------------------------------------------------- function [pTabs, MatchInd] = i_resetTableList(ud) %-------------------------------------------------------------------------- % Get the existing matching between tables and optim inputs pMatch = mbcpointer(1, length(ud.OptimValueNames)); pMatch(ud.MatchInd > 0) = ud.pTabs(ud.MatchInd(ud.MatchInd > 0)); % Get the existing import status boolImport = ud.ItemIncluded; % Find the list of possible tables pTabs = i_findTables(ud); OutDataNames = pveceval(pTabs, @getname); OutDataIcons = pveceval(pTabs, @i_getIconfile); % Set the new possible tables ud.hMatchTable.Peer.setAvailableDataItems(OutDataNames, OutDataIcons); % Setting the possible tables may screw up the existing matching. Need to % refill the table to set the matching back to what it was. MatchInd = findptrs(pMatch, pTabs); ud.hMatchTable.Peer.setItemInformation(... ud.OptimValueNames, MatchInd-1, boolImport); %-------------------------------------------------------------------------- function iconFile = i_getIconfile(table) %-------------------------------------------------------------------------- iconFile = cgrespath(iconfile(table));