UI doesn't show buttongroups but doesn't give any error

조회 수: 5 (최근 30일)
Arda Ozdogru
Arda Ozdogru 2022년 4월 9일
댓글: Voss 2022년 4월 10일
Hello everyone, I am trying to dynamically create a UI with changing number of buttongroups and radiobuttons. However, when I run it nothing comes up in the uifigure. Can someone help me sorts this out? I am sharing the code.
% inputs to the function
TotalSong = 3;
TotalOptionNo = 5;
fig = uifigure();
for i = 1:TotalSong
y = 1-(1-0.7/TotalSong)*i/TotalSong
if y<0
y = 0;
end
bg(i).bg = uibuttongroup(fig,'Title',['Song' int2str(i)], 'Visible','off',...
'Position',[0 y 1 0.7/TotalSong],...
'Units', 'normalized');
for k = 1:TotalOptionNo
rb(k).(['bg' int2str(i)]) = uiradiobutton(bg(i).bg,...
'Value', 0,...
'Text', int2str(k),...
'Position',[(1-0.3)*k/TotalOptionNo 0 0.1 0.5]);
end
bg(i).bg.Visible = 'on';
end

채택된 답변

Voss
Voss 2022년 4월 9일
There are a couple of reasons why this is happening, both of which are related to the 'Units' of the components.
First, when you create the uibuttongroup:
bg(i).bg = uibuttongroup(fig,'Title',['Song' int2str(i)], 'Visible','off',...
'Position',[0 y 1 0.7/TotalSong],...
'Units', 'normalized');
you have to specify the 'Units' before the 'Position':
bg(i).bg = uibuttongroup(fig,'Title',['Song' int2str(i)], 'Visible','off',...
'Units', 'normalized', ...
'Position',[0 y 1 0.7/TotalSong]);
If you put 'Position' first, MATLAB interprets that position in terms of the default units, which is pixels, and then converts that pixel position into normalized Units when it parses the 'Units','normalized' input arguments. Specifying 'Units' first prevents this conversion.
Making that change should make the uibuttongroups show up, but you will still see no actual uiradiobuttons because there's also a problem with their Units/Positions.
uiradiobuttons' Positions are in pixel units, and there's nothing you can do - as far as I know - to change their units (they don't have a 'Units' property at all). But here, you appear to want to set their Positions as if their Units are 'normalized', so to do that, you have to figure out what the position (in pixels) of a uiradiobutton should be, given the position of the parent uibuttongroup (and, since that is in 'normalized' units, you also need the position of the uifigure).
Here's how the corresponding code to do that would look:
TotalSong = 3;
TotalOptionNo = 5;
fig = uifigure();
fig_wh = fig.Position([3 4]); % uifigure width and height in pixels
bg_wh = [1 0.7/TotalSong]; % uibuttongroup width and height in 'normalized' units
rb_wh = fig_wh.*bg_wh.*[0.1 0.5]; % uiradiobutton width and height in pixels
for i = 1:TotalSong
y = 1-(1-0.7/TotalSong)*i/TotalSong;
if y<0
y = 0;
end
% specify Units before Position:
bg(i).bg = uibuttongroup(fig,'Title',['Song' int2str(i)], 'Visible','off',...
'Units', 'normalized', ...
'Position',[0 y bg_wh]);
for k = 1:TotalOptionNo
% uiradiobutton x-coordinate (left), in pixels:
rb_x = (1-0.3)*k/TotalOptionNo*bg_wh(1)*fig_wh(1);
rb(k).(['bg' int2str(i)]) = uiradiobutton(bg(i).bg,...
'Value', 0,...
'Text', int2str(k),...
'Position',[rb_x 0 rb_wh]);
end
bg(i).bg.Visible = 'on';
end
  댓글 수: 2
Arda Ozdogru
Arda Ozdogru 2022년 4월 10일
Thank you for teaching me. Code works really good when I set the size of figure beforehand.
Voss
Voss 2022년 4월 10일
You're welcome!

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

추가 답변 (1개)

Dave B
Dave B 2022년 4월 9일
편집: Dave B 2022년 4월 9일
The problem you're running into is just the order in which you set Units and Position:
bg(i).bg = uibuttongroup(fig,'Title',['Song' int2str(i)], 'Visible','off',...
'Position',[0 y 1 0.7/TotalSong],...
'Units', 'normalized');
This sets the Position in the default Units of uibuttongroup (which are pixels when that uibuggongroup is in a uifigure) and then sets the Units to normalized. So the buttongroups exist in your figure, but they are really tiny (hence, no error).
Swapping to setting the Units first should do the trick:
bg(i).bg = uibuttongroup(fig,'Title',['Song' int2str(i)], 'Visible','off',...
'Units', 'normalized', ...
'Position',[0 y 1 0.7/TotalSong]);
  댓글 수: 1
Arda Ozdogru
Arda Ozdogru 2022년 4월 10일
Thank you for detailed explanation, I accepted @_ answer as a fellow user but both answers are the same :)

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

카테고리

Help CenterFile Exchange에서 Develop uifigure-Based Apps에 대해 자세히 알아보기

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by