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',...
Walter Roberson
Walter Roberson 2017년 12월 28일
You would only get that error if your code assigning to box(i) had not executed in that workspace.

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

추가 답변 (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