How to resize uicontol when GUI is dock ? Programatically

9 次查看(过去 30 天)
Hello,
I have a customed made GUI which has many uicontrol buttons. One of the button when pushed dock the open figure to matlab workspace. Function is given below. My problem is whenever the GUI is dock control buttons comes to close to each other, some even overlap. It appears that GUI figure has adjusted its axis automatically but pushbuttons haven't. So please tell me how can I resize all buttons also along with figure. Thank you.
dock = uicontrol(figure,'Style','Pushbutton','FontSize',12,...
'String','Dock',...
'Units','Normalized',...
'Position',[.91 .96 .08 .03],...
'Callback',@dock_callback);
function dock_callback(handlesobj,~)
set(gcf, 'WindowStyle', 'docked');
set(gcf,'CurrentAxes')
end

回答(1 个)

Jayanti
Jayanti 2025-2-13
编辑:Jayanti 2025-2-13
Hi,
To ensure that components within a figure window automatically adjust when the window is resized-
  1. Set the “Units” property of all components inside the figure to “normalized”. This approach allows components to resize proportionally with the figure.
  2. Alternatively, you can use the “SizeChangedFcn” property of the figure. This property lets you specify a callback function that executes whenever the figure is resized, allowing to dynamically adjust the positions and sizes of UI controls. However, when defining a “SizeChangedFcn” callback, it is important to make the figure visible only after setting up the UI components like the following code.
f = figure('Visible','off','SizeChangedFcn',@sbar);
u = uicontrol('Style','edit','Tag','StatusBar');
dock = uicontrol(figure,'Style','Pushbutton','FontSize',12,...
'String','Dock',...
'Units','Normalized',...
'Position',[.91 .96 .08 .03],...
'Callback',@dock_callback);
f.Visible = 'on';
This will avoid triggering the “SizeChangedFcn” callback before the controls are fully initialized.
Please refer to the documentation link below which shows how to calculate new positions and sizes of UI component. You can adjust this logic for other components, so they do not overlap:
Hope this will be helpful!

类别

Help CenterFile Exchange 中查找有关 App Building 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by