How to create a dynamic number of aligned checkboxes and edit textboxes and how to tag them? Order of input arguments in uicontrol?

20 次查看(过去 30 天)
Hey Guys,
i have to create a GUI that adapts to the users input! So if the user puts is a 5, i would like to have five aligned checkboxes and edit textboxes. The names of the static textboxes as well as the Tags of the checkboxes shall be dependent of the input number given by the user. And i woould like to pack it into a panel.
I created this code:
stages = 5;
fig = figure('Visible','off');
box = zeros(stages,1);
txtbox = zeros(stages,1);
panel = uipanel('parent',fig,...
'Title','Choose stages to plot',...
'position',[.01 .05 .25 .95]);
A=zeros(stages,1);
for i = 1:stages
box(i,1) = uicontrol('parent',panel,'style','checkbox',...
'string',i,...
'position',[20 360-i*25 40 20],...
'callback','A(i,1)=1');
txtbox = uicontrol(fig,'Style','text',...
'String','Select ',...
'Position',[100 360-i*25 40 20]);
end
set(fig,'Visible','on')
Some questions to it.
1) How can i put the static text into the panel and align it with the checkboxes? i cannot find the order of input arguments for uicontrol so i dont know where to put 'parent' and panel as arguments.
2) How can i tag the checkboxes according to the number...so first checkbox shall have the tag 'checkbox1', the second 'checkbox2' and so on....
3) Is it possible to make the GUI window adapt to the number of created checkboxes so that it resizes in order to make all checkboxes visible or that the user is able to scroll down?
I know those are many questions:( i would be very glad to get answers!
Best regards, John

采纳的回答

Jan
Jan 2015-9-23
Question 1: put the static text into the panel
txtbox = uicontrol('parent', panel, ...
instead of txtbox = uicontrol(fig, ...
Question 2: dynamic tag
box(i) = uicontrol('parent', panel,'style','checkbox', ...
'tag', sprintf('checkbox%d', i), ...
Note: Be careful with tags. It might be easier to store a list of the handles in a variable and share it with the callbacks. See setappdata and guidata.
Question 3: Adjust the figure size.
The figure size can be easiliy adjusted by adjustig the figure size. Compute the required height of the figure and simply set it initally or finally:
set(fig, 'Visible', 'on', 'Position', [100, 100, computedHeight, Width]);
  9 个评论
Ahmed Khalil
Ahmed Khalil 2017-12-28
I'm so sorry to be a bother. I added setappdata(panel, 'box', box) but it gives this error:
Error using box Too many output arguments.
Error in multidesign2>edit1_Callback (line 101) design = uicontrol('parent',panel,'Style','edit',...

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Environment and Settings 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by