0

我想知道如何简化(修改) hSelecVarIndCallback 函数。我想避免在每种情况下重复选择字符串,并能够自动选择字符串单个值。

function example
fh = figure;
hPan = uipanel(fh,'title','Main Panel','Units','characters',...
    'Position',[2.307 0 52 30],'FontSize',10,'FontWeight',...
    'bold','FontAngle','italic');
hInd = uipanel('Parent',hPan,'title','Variable(s)','Units',...
    'normalized','Position',[0 0.587 1 0.344],'FontSize',9,...
    'FontAngle','italic');
hSelecVarInd = uicontrol(hInd,'Style','popupmenu','Units','normalized',...
    'String',{'Time','Var 1','Var 2','Var 3','Var 4','Var 5','Var 6'},...
    'Value',1,'Position',[0.25 0.275 0.5 0.5],'FontSize',9,'Callback',...
    @hSelecVarIndCallback,'BackgroundColor','white');
hDep = uipanel(hPan,'title','Variable(s) Dependiente(s)','Units',...
    'normalized','Position',[0 0 1 0.5],'FontSize',9,'FontAngle','italic');
hDeplb = uicontrol(hDep,'Style','listbox','Units','normalized',...
                'String',{'Var 1','Var 2','Var 3','Var 4','Var 5','Var 6'},...
                'Max',2,'Min',0,'Value',1,...
                'Position',[0 0 1 1],'FontSize',9);
  function hSelecVarIndCallback(src,evt)

    val = get(hSelecVarInd,'Value');
if (val == 1)
    set(hDeplb,'String',{'Var 1','Var 2','Var 3','Var 4','Var 5','Var 6'});
elseif (val == 2)
   set(hDeplb,'String',{'Time','Var 2','Var 3','Var 4','Var 5','Var 6'});
elseif (val == 3)
   set(hDeplb,'String',{'Time','Var 1','Var 3','Var 4','Var 5','Var 6'});
elseif (val == 4)
   set(hDeplb,'String',{'Time','Var 1','Var 2','Var 4','Var 5','Var 6'});
   elseif (val == 5)
set(hDeplb,'String',{'Time','Var 1','Var 2','Var 3','Var 5','Var 6'});
   elseif (val == 6)
set(hDeplb,'String',{'Time','Var 1','Var 2','Var 3','Var 4','Var 6'});
else
set(hDeplb,'String',{'Time','Var 1','Var 2','Var 3','Var 4','Var 5'});   
end

  end
end
4

1 回答 1

2

也许您可以执行以下操作:

function example
    %# list of all values
    varNames = {'Time','Var 1','Var 2','Var 3','Var 4','Var 5','Var 6'};

    %# create GUI
    fh = figure;
    hSelecVarInd = uicontrol(hInd, 'Style','popupmenu', ...
        'Units','normalized', 'Position',[0.25 0.275 0.5 0.5], ...
        'String',varNames, 'Value',1, 'Callback',@hSelecVarIndCallback, ...
        'FontSize',9, 'BackgroundColor','white');
    %# ... the rest here ...

    function hSelecVarIndCallback(src,evt)
        %# get selected value
        val = get(src, 'Value');

        %# all but the current value (logical mask)
        idx = true( size(varNames) );
        idx(val) = false;

        %# set the listbox
        set(hDeplb, 'String',varNames(idx));
    end
end

截屏

于 2011-11-01T19:47:24.873 回答