Create contents of popup menu dynamically

조회 수: 2 (최근 30일)
Luffy
Luffy 2012년 7월 4일
댓글: Samuel Hirsbrunner 2016년 7월 23일
I want to create a popup menu with contents dynamically,based on value of a variable n in my code.
Example:- If n is 2,the contents should be Data1,Data2.
If n is 3,the contents should be Data1,Data2,Data3.
...................................................
If n is x,the contents should be Data1,Data2,......Datax
  댓글 수: 2
Jan
Jan 2012년 7월 4일
Ok. What have you tried so far and what problems occurred? Did you read "doc uicontrol" already?
Luffy
Luffy 2012년 7월 5일
Yeah I know about uicontrol,I tried this:
v = evalin('base','n'); % n is variable i explained in question.
switch v
Case 1
uicontrol('Style', 'popupmenu', ...
'String', {'Data1'}, ...
'Units', 'pixels', ...
'Position', [370, 60, 95, 21]);
Case 2
uicontrol('Style', 'popupmenu', ...
'String', {'Data1','Data2'}, ...
'Units', 'pixels', ...
'Position', [370, 60, 95, 21]);
Case 3
uicontrol('Style', 'popupmenu', ...
'String', {'Data1','Data2','Data3'}, ...
'Units', 'pixels', ...
'Position', [370, 60, 95, 21]);
end
But what if value of n is 100,I can't go on writing switch cases.

댓글을 달려면 로그인하십시오.

채택된 답변

Mark Whirdy
Mark Whirdy 2012년 7월 4일
Hi Luffy
Not 100% sure I understand the degree of dynamism you're going for but stick the code below underneath whereever n changes (assuming handles is in scope there). Here you specify the menu contents cell array explicity in each case - does this fit your use-case?
switch n case 1 set(handles.popupmenu1,'String',{'Data1';'Data2'},'Value',1); case 2 set(handles.popupmenu1,'String',{'Data1';'Data2';'Data3'},'Value',1); end
Alternatively, if for some reason you're defining the contents to be a cellaray of 'Data#' whose length = n, then the code below should do it
n = 4; myarray = strcat('Data',cellfun(@num2str,num2cell((1:n)'),'UniformOutput',false)) set(handles.popupmenu1,'String',myarray,'Value',1);
All the best Mark
  댓글 수: 2
Luffy
Luffy 2012년 7월 5일
@Mark:thank you,but could you tell me that 'Value',1 in
set(handles.popupmenu1,'String',myarray,'Value',1) mean
Jan
Jan 2012년 7월 5일
Please read "doc uicontrol". The 'Value' is the initially selected item.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Jan
Jan 2012년 7월 5일
String = sprintf('Data%d#', 1:n);
String(end) = [];
CString = regexp(String, '#', 'split');
uicontrol('Style', 'popupmenu', ...
'String', String, ...
'Units', 'pixels', ...
'Position', [370, 60, 95, 21]);
  댓글 수: 3
Mus'ab Ahmad
Mus'ab Ahmad 2015년 8월 18일
Thanks Jan
Samuel Hirsbrunner
Samuel Hirsbrunner 2016년 7월 23일
finde this on my researches... I may shorten the code again:
String = sprintf('Data%d\n',1:size(Data))
by adding "\n" it made a new line on each value. so no need for the regesp() at least i think this works...

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Migrate GUIDE Apps에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by