MATLAB Answers

0

Is it possible to create "x" amount of objects with pop-up menus in GUI depending on the user input?

David (degtusmc) さんによって質問されました 2013 年 6 月 28 日
Hello everyone,
I am in the process of designing a Matlab GUI and I was wondering if what I want to do is even possible. If it is, I would appreciate if I can be pointed in the right direction.
The user will input a number in a text box. This number will be the amount of objects that will be created (inside the GUI). Each object will have two different menus with additional options. Am I able to accomplish this using Matlab GUI?
Thank you in advance for any help provided.

  0 件のコメント

サインイン to comment.

3 件の回答

回答者: Tom
2013 年 6 月 28 日
編集済み: Tom
2013 年 6 月 29 日
 採用された回答

You can do it programatically, using the UICONTROL function
function Create_Panels(varargin)
%tile a series of panels, each containing two listboxes
%argument 1: number of panels to create (otherwise default of 6)
if nargin > 0
nPanels = varargin{1};
if ~isnumeric(nPanels) || numel(nPanels) ~= 1 || nPanels < 1
error('please enter an integer greater than 0')
else
nPanels = round(nPanels);
end
else
nPanels = 6;
end
%create figure
figPos = [100 50 1000 600];
F = figure('Position',figPos);
%gaps between panels
horzSpc = 10;
vertSpc = 10;
%arrange tiling of panels
nHorz = ceil(sqrt(nPanels));
nVert = round(nPanels/nHorz);
[gridHorz gridVert] = meshgrid(1:nVert,1:nHorz);
%calculate panel size
panelHeight = round((figPos(4) - vertSpc) / nVert -vertSpc);
panelWidth = round((figPos(3) - horzSpc) / nHorz -horzSpc);
u = zeros(nPanels,1);
for n = 1:nPanels
x = horzSpc + (gridVert(n)-1)*(panelWidth + horzSpc);
y = vertSpc + (gridHorz(n)-1)*(panelHeight + vertSpc);
u(n) = Panel_Gen(x,y,panelHeight,panelWidth);
set(u(n),'Title',sprintf('Panel %d',n))
end
function u = Panel_Gen(x,y,h,w)
u = uipanel('Units','Pixels',...
'Position',[x y w h]);
%listboxes:
uicontrol('Style','popup',...
'Parent',u,...
'Position',[10, 10, ((w-10)/2)-10 h-30],...
'String',{'One', 'Two' ,'Three'})
uicontrol('Style','popup',...
'Parent',u,...
'Position',[((w-10)/2) + 10, 10, ((w-10)/2)-10, h-30],...
'String',{'Uno', 'Dos', 'Tres'})

  3 件のコメント

Great! Thank you for the rapid response. Now I know it is possible to do and I will give it a shot. Is it OK if I get back to you if further questions come up?
Tom
2013 年 6 月 29 日
Sure, but if it's not directly related to my code then I'd ask it as a new question so more people have the chance to look at it.
Thank you Tom. I tried your suggestion and it worked. Is there a way to create the panels under the input text box, rather than opening new windows (figures)? The reason, I am asking is because I would like to have more control over the amount of objects created (i.e. adding or removing panels).

サインイン to comment.


回答者: KIRAN kumar 2013 年 6 月 29 日

yeah the above one works try it!!!

  0 件のコメント

サインイン to comment.


回答者: Steven
2013 年 7 月 8 日

great

  0 件のコメント

サインイン to comment.



Translated by