www.gusucode.com > mbcmodels 工具箱 matlab 源码程序 > mbcmodels/@mbcinputfactor/gui_InputSetup.m

    function [InputsOut,ok]=gui_InputSetup(Inputs,action,figh,varargin)
%GUI_INPUTSETUP2   GUI for editing model input factors
%
%  [MODEL, OK] = GUI_INPUTSETUP( MODEL )
%  [MODEL, OK] = GUI_INPUTSETUP( MODEL, ACTION, ENABLE, TITLE )
%

%  Copyright 2000-2014 The MathWorks, Inc. and Ford Global Technologies, Inc.

if nargin<2
    action='figure';
elseif nargin<3
    figh=gcf;
end

switch lower(action)

    case 'figure'
        % not defined
        if nargin>2
            varargin=[{figh} varargin];
        end
        [InputsOut,ok]=i_createfig(Inputs,varargin{:});
    case 'layout'

        % create runtime pointer 
        p = xregGui.RunTimePointer;
        p.info = Inputs;
        [InputsOut, ok]=i_createlyt(figh,p,varargin{:});
        
    case 'isdirty'
        % external API function for asking if the model definition is applied
        udh=get(get(get(get(figh,'center'),'center'),'south'),'elements');
        udh=udh{2};
        ud=get(udh,'UserData');
        InputsOut=ud.changedinputs;
        ok=1;
        
    case 'update'
        pData = get(figh,'UserData');
        changeInputs(pData,Inputs,varargin{:});
        InputsOut = Inputs;
        ok = true;
    case 'finalize'
        pData = get(figh,'UserData');
        [InputsOut,ok] = iFinalize(Inputs,pData);
end
return





function [lyt, pData]=i_createlyt(figh,p,Enable)

pData = xregGui.RunTimePointer;
ud.pointer=p;
ud.callback='';
Inputs=p.info;
nf= length(Inputs);

trans=mbcinputfactor.ValidTransforms;
trans{1} = 'None';

if isgraphics(figh,'figure')
    bgcol=get(figh,'Color');
else
    bgcol=get(figh,'BackgroundColor');
end
% set up table look
t=xregtable(figh,...
    'frame.visible','off',...
    'frame.hborder',[0 0],...
    'frame.vborder',[2 2],...
    'defaultcellformat','%g',...
    'defaultcelltype','uipushbutton',...
    'cols.size',80,...
    'cols.spacing',2,...
    'rows.spacing',2,...
    'cells.defaultinterruptible','off',...
    'cells.rowselection',[1 1],...
    'cells.colselection',[1 5],...
    'cells.type','uitext',...
    'cells.string',{'Symbol','Min','Max','Transform','Signal'},...
    'cells.fontweight','bold',...
    'cells.backgroundcolor',bgcol,...
    'rows.fixed',1,...
    'zeroindex',[2 1],...
    'cells.defaultbackgroundcolor',[1 1 1],...
    'cells.rowselection',[2 nf+1],...
    'cells.colselection',[1 3],...
    'cells.type','uiedit',...
    'cells.horizontalalignment','right',...
    'cells.colselection',[4 4],...
    'cells.type','uipopupmenu',...
    'cells.string',{trans},...
    'cells.value',1,...
    'cells.rowselection',[2 nf+1],...
    'cells.colselection',[5 5],...
    'cells.type','uiedit',...
    'cells.colselection',[1 5],...
    'cells.rowselection',[1 nf+1],...
    'cells.horizontalalignment','left',...
    'position',[0 0 300 50],...
    'redrawmode','basic');
t.redrawmode='normal';

uh = mbcgui.widget.Spinner('Parent', figh,...
    'Callback',{@i_NumFactors,pData},...
    'Min',1,...
    'Value',nf,...
    'Rule','int');
lbl = xregGui.labelcontrol('Parent', figh, ...
    'String', 'Number of factors:', ...
    'LabelSizeMode', 'absolute', ...
    'LabelSize',100, ...
    'ControlSize', 65, ...
    'Control', uh);



ud.trans= trans;
ud.parent=figh;
ud.table=t;
ud.callback='';
ud.changedinputs=0;
ud.numfactors = uh;

% callbacks
t.cellchangedcallback={@i_cellchng,pData,Inputs};

lyt = xreggridlayout(figh, ...
    'dimension',[2 1],...
    'correctalg','on', ...
    'gapy',10, ...
    'rowsizes',[20 -1], ...
    'elements',{lbl,t}, ...
    'userdata', pData);


ud=i_redrawtable(ud,Enable);
pData.info = ud;


return



function ud=i_redrawtable(ud,Enable)
% repopulate table with values from m

Inputs=ud.pointer.info;
t=ud.table;

if nargin>1 
    en = mbconoff(Enable);
    
    % turn off signal names edit
    set(t,...
        'cells.rowselection',[2 length(Inputs)+1],...
        'cells.colselection',[5 5],...
        'cells.enable',en);
    set(ud.numfactors,'Enable',en);
end



fnms = getList(Inputs,'Name');
% symbols
symb = getList(Inputs,'Symbol');
[~,ytr]= getcode(Inputs);


ValidTrans = mbcinputfactor.ValidTransforms;
% convert ytr to values
for n=1:length(ytr)
    ytr{n}=find(strcmp(char(ytr{n}),ValidTrans));
end
% convert any unrecognised transforms to no transform
ytr(cellfun('isempty',ytr)) = {1};
ytr=cat(1,ytr{:});

nf=length(Inputs);
if isempty(symb)
    symb=cellstr(char((65:64+nf)'));
end
Xbnds= getcode(Inputs);

t.redrawmode='basic';
t(:,1).string=symb;
t(:,2:3).value= Xbnds;
t(:,4).value=ytr;
t(:,5).string= fnms(:);

t.redrawmode='normal';
t.redraw;

return



function i_firecb(cbstr,ud)
% parse callback string and execute it

if ~isempty(cbstr)
    if ischar(cbstr)
        % parse for %MODEL% and %HOOK INTERNAL% and %POINTER%

        if strcmp(cbstr,'%HOOK_INTERNAL%');
            % execute internal function to do apply enable status
            i_applyenable(ud);
            return
        end

        ptr=ud.pointer;
        pcs=strfind(cbstr,'%');
        go=1;
        needobj=0;
        needval=0;
        while (go<=(length(pcs)-1))
            cmp=cbstr(pcs(go)+1:pcs(go+1)-1);
            if strcmp(cmp,'POINTER')
                needval=1;
                cbstr=[cbstr(1:pcs(go)-1) 'XX_POINTER_XX' cbstr(pcs(go+1)+1:end)];
                go=go+2;
                pcs=pcs+6;
            elseif strcmp(cmp,'MODEL')
                needobj=1;
                cbstr=[cbstr(1:pcs(go)-1) 'XX_MODEL_XX' cbstr(pcs(go+1)+1:end)];
                go=go+2;
                pcs=pcs+6;
            else
                go=go+1;
            end
        end

        if needobj
            assignin('base','XX_MODEL_XX',ptr.info);
        end
        if needval
            assignin('base','XX_POINTER_XX',ptr);
        end
        evalin('base',cbstr);

        % clear up base workspace
        evalin('base','clear(''XX_MODEL_XX'',''XX_POINTER_XX'');');
    else
        % Function handle
        if ~iscell(cbstr)
            cbstr={cbstr};
        end
        evt.NewModel=ud.pointer.info;
        if length(cbstr)>1
            feval(cbstr{1}, ud.mainlyt, evt, cbstr{2:end})
        else
            feval(cbstr{1}, ud.mainlyt, evt);
        end
    end
end
return



function i_applyenable(ud)

% decide enable status of apply/revert buttons
el=get(get(get(ud.mainlyt,'center'),'south'),'elements');
if ud.changedmodel
    set(el{1},'Enable','on');
else
    set(el{1},'Enable','off');
end
return



function [Inputs,ok]=i_createfig(Inputs,Enable,Title)
if nargin<3
    Title= 'Factor Setup';
end
if nargin<2
    Enable= 1;
end

dlg = mbcgui.container.Dialog( 'Name', Title,...
    'Size',[470 300],...
    'Buttons','OK_CANCEL_HELP',...
    'HelpCode','xreg_modelInputSetup'  );
figh = dlg.Figure;

p=xregpointer(Inputs);
[lyt, pData]=i_createlyt(figh,p,Enable);
dlg.Content = lyt;
dlg.ValidationFcn = @() iValidate(pData);
closeMode = dlg.showDialog(  );
if strcmp(closeMode,'OK')
    % check info
    [Inputs,ok] = iFinalize(Inputs,pData);
else
    ok = false;
end

freeptr(p);
delete(dlg);



function i_NumFactors(h,~,pData)

ud= pData.info;
Inputs=ud.pointer.info;
nfold= length(Inputs);
nf = h.Value;
if nf<nfold
    Inputs = Inputs(1:nf);
    
    % delete rows
    set(ud.table,'Rows.Number',nf+1)
    ud.table.redraw;
    ud.pointer.info= Inputs;
    
    
elseif nf>nfold

    try
        Inputs = iGetInputs(ud);
    catch ME
        hef=xregerror('Input Error',ME.message);
        drawnow
        uiwait(hef)
        h.Value = nfold;
        return 
    end

    % create unique new factor settings to avoid conflicts in names
    % symbol stem will be
    Base = regexp(Inputs(end).Symbol,'^([A-Za-z]+)','match');
    for i = nfold+1:nf
        S = [Base{1}, num2str(i)];
        Inputs(i,1) = mbcinputfactor('Symbol',{S},'Range',[0 100]);
    end
    
    ud.pointer.info= Inputs;

    % add new rows to table
    t = ud.table;
    set(t,'rows.number',nf+1)
    set(t,...
        'cells.rowselection',[nfold+2 nf+1],...
        'cells.colselection',[1 3],...
        'cells.type','uiedit',...
        'cells.colselection',[4 4],...
        'cells.type','uipopupmenu',...
        'cells.string',{ud.trans},...
        'cells.value',1,...
        'cells.colselection',[5 5],...
        'cells.type','uiedit',...
        'cells.colselection',[1 5],...
        'cells.horizontalalignment','left')

    ud=i_redrawtable(ud);
    pData.info = ud;
end


function [Inputs,ok]=i_checkdata(ud)

Inputs=iGetInputs(ud);
% check strings are valid and all different
AssertValidLabels(Inputs);

ok=1;
return



function [mm,g]= i_getcode(t)

%minmax
mm=t(:,2:3).value;
%ytrans
ytr=t(:,4).value;
ytrstr=t(1,4).string;

t.redrawmode='basic';
g = cell(length(ytr),1);
for i=1:length(ytr)
    if ytr(i)==1
        % first item on popup menu is 'None' for no transformation
        g{i}= '';
    else
        % other items are the actual expressions for coding transformation
        g{i}= ytrstr{ytr(i)};
    end
end


function Inputs = iGetInputs(ud)

tstr= get(ud.table,'String');
S= tstr(:,1);
N= tstr(:,end);
[bnds,g] = i_getcode(ud.table);
% make input object from current test
Inputs = mbcinputfactor([S num2cell(bnds),g,N]);


function i_cellchng(t,eventData,pData,~)

ud = pData.info;
% add/change callback depending on column number
if eventData.Column ==1
    % get all symbols and insert into model
    % symbol changing interface??
    symb=t(:,1).string;
    if ~iscell(symb)
        symb={symb};
    end
    Inputs=ud.pointer.info;
    % check for empty strings - reject immediately
    if any(cellfun('isempty',symb))
        symb = getList(Inputs,'Symbol');
        t(:,1).string=symb;
    else
        if ~isempty(gcbo);
            si= get(gcbo,'String');
            tstr = [strfind(si,'_') strfind(si,'\') strfind(si,'^') strfind(si,'{') strfind(si,'}')];
            if  ~isempty(tstr)
                style= struct('WindowStyle','modal','Interpreter','TeX');
                warndlg(['The symbol contains TeX expressions and will be displayed as "',si,...
                    '" in equation expressions.'],'Symbol Definition',style);
            end
        end
        Inputs = setList(Inputs,'Symbol',symb);
        ud.pointer.info=Inputs;
        ud.changedinputs=1;

        pData.info = ud;
        if ~isempty(ud.callback)
            i_firecb(ud.callback,ud);
        end
    end

else
    % disable target ranges for now
    t.redrawmode='normal';
    t.redraw;

    ud.changedinputs=1;
    pData.info = ud;
    if ~isempty(ud.callback)
        i_firecb(ud.callback,ud);
    end
end

function [Inputs,ok] = iFinalize(Inputs,pData)
ud=pData.info;
try
    [Inputs,ok]=i_checkdata(ud);
catch ME
    ok=0;
    hef=xregerror('Input Error',ME.message);
    drawnow
    uiwait(hef)
end


function ok = iValidate(pData)
%iValidate check final values

[~,ok] = iFinalize([],pData);

function changeInputs(pData,Inputs,varargin)

ud = pData.info;
OldInputs = ud.pointer.info;
nf  = length(Inputs);
nfold= length(OldInputs);

if nf<nfold
    % delete rows
    set(ud.table,'Rows.Number',nf+1)
    
    
elseif nf>nfold

    % add new rows to table
    t = ud.table;
    set(t,'rows.number',nf+1)
    set(t,...
        'cells.rowselection',[nfold+2 nf+1],...
        'cells.colselection',[1 3],...
        'cells.type','uiedit',...
        'cells.colselection',[4 4],...
        'cells.type','uipopupmenu',...
        'cells.string',{ud.trans},...
        'cells.value',1,...
        'cells.colselection',[5 5],...
        'cells.type','uiedit',...
        'cells.colselection',[1 5],...
        'cells.horizontalalignment','left')


end
set(ud.numfactors,'Value',nf);

ud.pointer.info= Inputs;
ud=i_redrawtable(ud);
pData.info = ud;

pData.info.pointer.info = Inputs;
pData.info =i_redrawtable(pData.info,varargin{:});