Placing radiobuttons with relative locations

How can I place buttons created by uicontrol() in a relative location, as opposed to absolute location, within a ui window?
It makes no sense to me that a uibuttongroup can be placed in a relative location, but the actual buttons can not. This is a problem when resizing a window.
For example, I can do:
button_group_1 = uibuttongroup('Visible','on','Position',[ 0.4 0 0.4 0.4 ]);
button_1 = uicontrol( button_group_1,'Style','radiobutton','String','button_1','Position',[ 10 50 100 50 ]);
But I can not do:
button_group_1 = uibuttongroup('Visible','on','Position',[ 0.4 0 0.4 0.4 ]);
button_1 = uicontrol( button_group_1,'Style','radiobutton','String','button_1','Position',[ 0.4 0 0.4 0.4 ]);
Could someone help me implement buttons in a relative position so that a window can be resized and their locations still make sense?

 Risposta accettata

Hi Ryan1234321,
When creating GUIs by command without the use of GUIDE I do place all elements in absolute position. I take care that it fits well on my screen first. It is no problem to do so on any other machine, but GUI is going to be showed while all GUI elements are rescaled to a new window size.
My GUIs often contain a global variable "GUI" that is of type struct containing all figure handles where "GUI.fh" is the figure handle of the parent figure.
For changing the window size it is very effective to change the unit of fonts and general lengths into normalized:
function resize(~,~)
% Set all 'Units'-property within figure to 'normalized in order to
% allow for scaling
set(findall(GUI.fh,'Units','pixels'),'Units','normalized');
set(findall(GUI.fh,'FontUnits','points'),'FontUnits','normalized');
end
Thus, final lines in most GUIs are after building them the following:
drawnow();
resize();
set(GUI.fh,'ResizeFcn',@resize);
Kind regards,
Robert

4 Commenti

Robert,
Thank you for your answer and patience helping me with my issue. I have tried the solutions that you suggest and they have not worked well to solve my issue. Below I have included an example script creating a simple GUI and I will touch on a few things that are not working well for me.
gui_window = figure;
% button group 1 with entry window
button_group_1 = uibuttongroup('Visible','off',...
'Position',[0 0 0.2 0.4 ],...
'SelectionChangedFcn',...
@buttonSelection);
button_1 = uicontrol(button_group_1,...
'Style','radiobutton',...
'String','button 1',...
'Visible', 'on',...
'Position',[10 75 100 30]);
button_2 = uicontrol(button_group_1,...
'Style','radiobutton',...
'String','button 2',...
'Position',[10 50 100 30]);
button_group_1_entry = uicontrol('Style','edit',...
'String','Entry',...
'Position',[10 25 100 30]);
button_group_1.Visible = 'on';
% button group 2 with entry window
button_group_2 = uibuttongroup('Visible','off',...
'Position',[0.2 0 0.2 0.4 ],...
'SelectionChangedFcn',...
@buttonSelection);
button_3 = uicontrol(button_group_2,...
'Style','radiobutton',...
'String','button 3',...
'Visible', 'on',...
'Position',[10 75 100 30]);
button_4 = uicontrol(button_group_2,...
'Style','radiobutton',...
'String','button 4',...
'Position',[10 50 100 30]);
button_group_2_entry = uicontrol('Style','edit',...
'String','Entry',...
'Position',[110 25 100 30]);
button_group_2.Visible = 'on';
drawnow();
resize();
set(gui_window,'ResizeFcn',@resize);
function resize(~,~)
% Set all units to normalized
set( findall(gui_window,'Units','pixels'),'Units','normalized');
set( findall(gui_window,'FontUnits','points'),'FontUnits','normalized');
end
function buttonSelection( source, event )
disp(['Previous: ' event.OldValue.String]);
disp(['Current: ' event.NewValue.String]);
end
First, placing the button groups in absolute positions does not work, as they simply do not show up at all in the output window unless they are placed with relative coordinates.
Secondly, I want to have an entry box associated with each of the button groups and I have not been able to do this, as the entry boxes do not move with the button groups when the window is resized.
Third, the gui_window variable within the resize() function is not defined when used in this way, even when it is made to be a global variable in the script. It is not a struct, but I would expect it would behave similarly to a struct containing the figure handles.
I hope I have described my difficulties adequately, if you could help me address these specific questions again, I would greatly appreciate it.
Ryan
  1. uicontrol settings for button groups regarding units are set to "normalized" by default. You can change that property to 'pixels' in order to place your button groups in absolute values
  2. You have to provide the parent handle pointing to the corresponding button group to associate the edit box.
  3. The GUI itself should be a function 'myGUI'. "Global" variables within functions can be used to exchange data throughout private functions within 'myGUI'. Otherwise use guidata or explicit function calls.
I changed your example according to my suggestions. I hope you get a better idea. Of course there are other ways how to organize GUI data.
function myGUI(~)
gui.fh = figure;
% button group 1 with entry window
gui.btnGrp1.fh = uibuttongroup('Visible','off',...
'Unit','Pixels',...
'Position',[10 10 120 160 ],...
'SelectionChangedFcn',@buttonSelection);
gui.btnGrp1.btn(1).fh = uicontrol(gui.btnGrp1.fh,...
'Style','radiobutton',...
'String','button 1',...
'Visible', 'on',...
'Position',[10 75 100 30]);
gui.btnGrp1.btn(2).fh = uicontrol(gui.btnGrp1.fh,...
'Style','radiobutton',...
'String','button 2',...
'Position',[10 50 100 30]);
gui.btnGrp1.edit.fh = uicontrol(gui.btnGrp1.fh,...
'Style','edit',...
'String','Entry',...
'Position',[10 25 100 30]);
gui.btnGrp1.fh.Visible = 'on';
% button group 2 with entry window
gui.btnGrp2.fh = uibuttongroup('Visible','off',...
'Unit','Pixels',...
'Position',gui.btnGrp1.fh.Position + [ gui.btnGrp1.fh.Position(1)+gui.btnGrp1.fh.Position(3)+10 0 0 0],...
'SelectionChangedFcn',@buttonSelection);
gui.btnGrp2.btn(1).fh = uicontrol(gui.btnGrp2.fh,...
'Style','radiobutton',...
'String','button 3',...
'Visible', 'on',...
'Position',[10 75 100 30]);
gui.btnGrp2.btn(2).fh = uicontrol(gui.btnGrp2.fh,...
'Style','radiobutton',...
'String','button 4',...
'Position',[10 50 100 30]);
gui.btnGrp2.edit.fh = uicontrol(gui.btnGrp2.fh,...
'Style','edit',...
'String','Entry',...
'Position',[10 25 100 30]);
gui.btnGrp2.fh.Visible = 'on';
drawnow();
resize();
set(gui.fh,'ResizeFcn',@resize);
function resize(~,~)
% Set all units to normalized
set( findall(gui.fh ,'Units','pixels'),'Units','normalized');
set( findall(gui.fh ,'FontUnits','points'),'FontUnits','normalized');
end
function buttonSelection( ~, event )
disp(['Previous: ' event.OldValue.String]);
disp(['Current: ' event.NewValue.String]);
end
end
Kind regards,
Robert
Robert,
Thank you for your answer, I overlooked the ability to set the uibuttoncontrol's units to pixels and that I should pass the figure handle for the button group to the uicontrol() for the entry box.
The modifications you made worked very well.
Much appreciation,
Ryan
Thank you for your positive feedback. If you like my answer, please, vote for it by clicking on the "thumb up"-symbol.
In case it serves your needs and answers your question thoroughly, accept it.
Kind regards,
Robert

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su App Building in Centro assistenza e File Exchange

Prodotti

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by