MATLAB Answers

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

55 views (last 30 days)
John on 23 Sep 2015
Commented: Walter Roberson on 28 Dec 2017
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]);
for i = 1:stages
box(i,1) = uicontrol('parent',panel,'style','checkbox',...
'position',[20 360-i*25 40 20],...
txtbox = uicontrol(fig,'Style','text',...
'String','Select ',...
'Position',[100 360-i*25 40 20]);
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 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


Sign in to comment.

Accepted Answer

Jan on 23 Sep 2015
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]);


Show 6 older comments
Walter Roberson
Walter Roberson on 27 Dec 2017
k = 3; %for example
box_k = findobj(0, 'tag', sprintf('checkbox%d', k) )
It would be much easier to not do this: that instead of finding them by tag, to save a vector of handles somewhere at creation time, and then at execution time, retrieve the vector and index it. In terms of Jan's code:
box(i) = uicontrol('parent', panel,'style','checkbox', ...
setappdata(fig, 'box', box)
and then later
box = getappdata(fig, 'box');
k = 3;
box_k = box(k);
Ahmed Khalil
Ahmed Khalil on 28 Dec 2017
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',...

Sign in to comment.

More Answers (0)

Translated by