Clear Filters
Clear Filters

Expand GUI in App UIFigure by a button for to extend the showing

7 views (last 30 days)
Hi,
I'm trying to expand down part of the UIFigure of a MATLAB App by a button push.
As example:
from: - to ->
I tryied to change the weight of the figure but it does not work properly:
% IN THE BUTTON CALLBACK
posicio_actual = app.UIFigure.Position; % Exemple : 666 346 588 389 [posX, posY, amplada, llargada]
posicio_actual(4) = posicio_actual(4) + 150;
app.UIFigure.Resize = 'off';
app.UIFigure.AutoResizeChildren = 'off';
app.UIFigure.Position = posicio_actual;
But this is the result:
The below part does not show.
Thank you!

Accepted Answer

Voss
Voss on 15 Jul 2022
The Position of each component (specifically Position([1 2]), which are the x- and y-coordinates of the lower-left corner of the component) is measured from the bottom-left corner of its parent container (i.e., figure/uifigure/uipanel/whatever), so when the uifigure's height increases by 150 pixels, in order to do what you want, you actually need to move all the components in the uifigure up by 150 pixels at the same time. (Also, since the uifigure is expanding downward, it makes sense to move the bottom of the uifigure down by 150 pixels so that the top edge remains in the same place it was.)
Here is an example with a traditional figure, where the pushbutton toggles the state of the figure between "expanded" and "collapsed" (you can run it and make sure it does what you want, then apply the same logic to your app):
handles = struct();
handles.figure = figure('Units','pixels');
handles.top_ax = axes( ...
'Units','pixels', ...
'Position',[30 30 200 200]);
handles.bottom_ax = axes( ...
'Units','pixels', ...
'Position',[30 -120 400 90]);
handles.pushbutton = uicontrol( ...
'Units','pixels', ...
'Position',[260 30 70 26], ...
'String','Expand', ...
'Callback',{@cb_expand,true});
guidata(handles.figure,handles);
function cb_expand(src,~,do_expand)
handles = guidata(src);
dy = 150;
if ~do_expand
dy = -dy;
end
pos = get(handles.figure,'Position');
pos([2 4]) = pos([2 4])+[-dy dy];
set(handles.figure,'Position',pos);
pos = get(handles.top_ax,'Position');
pos(2) = pos(2)+dy;
set(handles.top_ax,'Position',pos);
pos = get(handles.bottom_ax,'Position');
pos(2) = pos(2)+dy;
set(handles.bottom_ax,'Position',pos);
pos = get(handles.pushbutton,'Position');
pos(2) = pos(2)+dy;
set(handles.pushbutton,'Position',pos);
if do_expand
new_str = 'Collapse';
else
new_str = 'Expand';
end
set(handles.pushbutton, ...
'String',new_str, ...
'Callback',{@cb_expand,~do_expand});
end

More Answers (0)

Categories

Find more on Develop uifigure-Based Apps in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!