Create contents of popup menu dynamically
Show older comments
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
Accepted Answer
More Answers (1)
Jan
on 5 Jul 2012
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 Comments
Carl
on 9 May 2013
Actually, the code can be simplified even further from Jan's answer. For a popup menu with multiple lines, you break the lines by the '|' character when feeding in a string to the 'uicontrol'. So just use this line of code:
String = sprintf('Data%d|',1:size(Data))
But Jan's answer is very elegant.
Mus'ab Ahmad
on 18 Aug 2015
Thanks Jan
Samuel Hirsbrunner
on 23 Jul 2016
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...
Categories
Find more on App Building in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!