Modify choices for a ListBox Callback

4 ビュー (過去 30 日間)
Julián Francisco
Julián Francisco 2011 年 11 月 1 日
I would like know how I could simplify(modify) the hSelecVarIndCallback function. I want to avoid the repetition of the choices string for each case and be able to get an automatic choice of the string individual value.
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

採用された回答

Walter Roberson
Walter Roberson 2011 年 11 月 1 日
S = {'Time','Var 1','Var 2','Var 3','Var 4','Var 5','Var 6'};
set(hDeplb, 'String', S(setdiff(1:length(S),val)) );
  1 件のコメント
Julián Francisco
Julián Francisco 2011 年 11 月 1 日
@Walter Roberson: Thank you so much for your answer. It is exactly what I need.

サインインしてコメントする。

その他の回答 (1 件)

Alex
Alex 2011 年 11 月 1 日
I don't fully understand what you mean by your question, but I'll take a guess at a solution.
function hSelecVarIndCallback(src,evt)
val = get(hSelecVarInd,'Value');
popup_contents = {'Time','Var 1','Var 2','Var 3','Var 4','Var 5','Var 6'};
popup_contents(val) = [];
set(hDeplb,'String',popup_contents);
end
  3 件のコメント
Alex
Alex 2011 年 11 月 1 日
I made a mistake in my answer. I used {} instead of ().
using:
popup_contents(val) = [];
instead will remove that element from the array completely, and not leave you with the empty element.
Julián Francisco
Julián Francisco 2011 年 11 月 1 日
@Alex: I suggest you to edit your answer to show the proper code.

サインインしてコメントする。

製品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by