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

4 visualizaciones (últimos 30 días)
Arda Ozdogru
Arda Ozdogru el 9 de Abr. de 2022
Comentada: Voss el 10 de Abr. de 2022
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

Respuesta aceptada

Voss
Voss el 9 de Abr. de 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 comentarios
Arda Ozdogru
Arda Ozdogru el 10 de Abr. de 2022
Thank you for teaching me. Code works really good when I set the size of figure beforehand.
Voss
Voss el 10 de Abr. de 2022
You're welcome!

Iniciar sesión para comentar.

Más respuestas (1)

Dave B
Dave B el 9 de Abr. de 2022
Editada: Dave B el 9 de Abr. de 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 comentario
Arda Ozdogru
Arda Ozdogru el 10 de Abr. de 2022
Thank you for detailed explanation, I accepted @_ answer as a fellow user but both answers are the same :)

Iniciar sesión para comentar.

Categorías

Más información sobre Migrate GUIDE Apps en Help Center y File Exchange.

Productos


Versión

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by