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

3 visualizzazioni (ultimi 30 giorni)
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

Risposta accettata

Voss
Voss il 9 Apr 2022
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 Commenti
Arda Ozdogru
Arda Ozdogru il 10 Apr 2022
Thank you for teaching me. Code works really good when I set the size of figure beforehand.

Accedi per commentare.

Più risposte (1)

Dave B
Dave B il 9 Apr 2022
Modificato: Dave B il 9 Apr 2022
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 Commento
Arda Ozdogru
Arda Ozdogru il 10 Apr 2022
Thank you for detailed explanation, I accepted @_ answer as a fellow user but both answers are the same :)

Accedi per commentare.

Categorie

Scopri di più su Migrate GUIDE Apps in Help Center e File Exchange

Prodotti


Release

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by