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

    function [hTable, UpdateFcn] = pRangeEditor(obj, hFig, pCS, Model, Callback) %#ok<INUSL>
%PRANGEEDITOR Create a table that edits the candidateset's range.
%
%   HTABLE = PRANGEEDITOR(OBJ, FIGURE, PCS, MODEL, CALLBACK) creates a table object
%   that edits the candidateset object held in the pointer pCS. MODEL must
%   contain an xregmodel object that is used to code the values the user
%   sets.  CALLBACK must contain a callback that is executed whenever the
%   range changes.
%
%   [HTABLE, FCN] = PRANGEEDITOR(...) also returns a function handle that
%   is used to update the table with new data.  The function must be passed
%   a new pointer to a candidate set and a new model.

%   Copyright 2007-2015 The MathWorks, Inc.


pUD = xregGui.RunTimePointer;

ud.pointer = pCS;
ud.model = Model;
ud.callback = Callback;

hTable = mbcwidgets.VariableEditorTable( ...
    'Parent', hFig, ...
    'ColumnWidth', 250, ...
    'ValueColumnCount', 2, ...
    'NameColumnHeader', 'Input Factor', ...
    'ValueColumnHeader', {'Min', 'Max'}, ...
    'Userdata', pUD);
ud.Table = hTable;
ud.VarChange = handle.listener(hTable, 'VariableChanged', {@iRangeChange, pUD});

% Create a context menu that will reset the range to the model's
uic = uicontextmenu('Parent', ancestor(hFig, 'figure'), 'Callback', {@iSetContextLabel, pUD});
ud.ResetMenu = uimenu(uic, ...
    'Label', 'Reset Range for Factor', ...
    'Callback', {@iRangeReset, pUD});
hTable.createCopyPasteMenus(uic);
hTable.TableContextMenu = uic;

pUD.info = ud;

iUpdateTable(pUD, pCS, Model);

UpdateFcn = @(P, M) iUpdateTable(pUD, P, M);




function iUpdateTable(pUD, pCS, Model)
ud = pUD.info;
ud.model = Model;
ud.pointer = pCS;
pUD.info = ud;

if ~isempty(pCS)
    fact = get(Model,'symbol');
    lims = limits(pCS.info);
    lims = invcode(Model,lims')';
    ud.Table.setVariables(fact, ones(length(fact), 2), num2cell(lims) );
end


function iRangeChange(src, evt, pUD)            %pCS, Model, Callback)
% Only continue if the edit was on a range column
if ~src.isEventInColumn(evt, {'Min','Max'})
    return
end

ud = pUD.info;

lims = src.getValueColumn({'Min', 'Max'});
lims = reshape([lims{:}], size(lims));

% Check the values are OK
if any(~isfinite(lims(:))) || any(diff(lims,[],2)<=0)
    % Reset table
    lims = limits(ud.pointer.info);
    lims = invcode(ud.model,lims')';
    src.setValueColumn({'Min', 'Max'}, num2cell(lims));
else
    lims = code(ud.model,lims')';
    ud.pointer.info = limits(ud.pointer.info, lims);
    xregcallback(ud.callback,src,[]);
end


function iSetContextLabel(src, evt, pUD)
ud = pUD.info;
R = ud.Table.getSelectedRows;
if isempty(R)
    set(ud.ResetMenu, 'Label', 'Reset Range for Factor');
elseif isscalar(R)
    fact = ud.Table.VariableNames;
    set(ud.ResetMenu, 'Label', sprintf('Reset Range for %s', fact{R}));
else
    set(ud.ResetMenu, 'Label', 'Reset Range for Factors');
end

% Save the rows to the userdata so we can directly use them to perform the
% reset operation.
set(ud.ResetMenu, 'UserData', R);


function iRangeReset(src, evt, pUD)
ud = pUD.info;
R = get(ud.ResetMenu, 'UserData');
tgt = gettarget(ud.model);
lims = limits(ud.pointer.info);
lims(R,:) = tgt(R,:);

ud.pointer.info = limits(ud.pointer.info, lims);

lims = invcode(ud.model,lims')';
ud.Table.setValueColumn({'Min', 'Max'}, num2cell(lims));


xregcallback(ud.callback,src,[]);