Making GUI with sub-elements resizable in a quick way
2 views (last 30 days)
Show older comments
When developing a GUI in guide, there is a global GUIDE option that let's you make the GUI resizable in a way that all sub-elements resize proprtionately with the figure.
However, I am given a programmatically generated GUI and I would like to modify the mfile so that it is resizable in the same way. Is there a quick way to do this, i.e., by modifying a single property of the parent figure, or do I have to write a ResizeFcn callback that manually alters the size/position of the sub-elements one by one?
0 Comments
Accepted Answer
per isakson
on 11 Jun 2014
Edited: per isakson
on 11 Jun 2014
Add this line at the end of the code, which defines the GUI
set( findall( fig_handle, '-property', 'Units' ), 'Units', 'Normalized' )
2 Comments
per isakson
on 25 Jun 2014
Edited: per isakson
on 25 Jun 2014
This behaves as expected both regarding resizing and manipulation with the slider
fh = figure;
hax = axes('Units','pixels');
surf(peaks)
slider_h = uicontrol('Style', 'slider',...
'Units', 'Pixels', ...
'Min',1,'Max',50,'Value',41,...
'Position', [400 20 120 20] );
set( slider_h, 'Callback', { @surfzlim, hax } );
set( findall( fh, '-property', 'Units' ), 'Units', 'Normalized' )
where
function surfzlim( hObj, event, ax ) %#ok<INUSL>
% Called to set zlim of surface in figure axes
% when user moves the slider control
val = 51 - get( hObj, 'Value' );
zlim( ax, [-val val] );
end
More Answers (2)
per isakson
on 6 Jun 2014
Edited: per isakson
on 6 Jun 2014
Based on a simple experiment with R2013a, I think this will do the trick
h = findobj( fh, '-property', 'Units' );
set( h, 'Units', 'Normalized' )
where fh is the handle of the GUI (figure).
I'm not sure about pro/cons regarding setting the unit property of the figure itself. I guess it doesn't hurt.
BTW, you might need to replace findobj by findall
1 Comment
chlor thanks
on 3 Aug 2016
Hi per, I want to try your code to resize the static text in my text box made in GUIDE, is this possible? Is so, should I put handles.statictext instead of fh in the code and where shall I insert this code? Thanks!
Image Analyst
on 6 Jun 2014
You can programmatically resize your GUI like this:
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
You can also use position instead of outerposition and the array is [left, top, width, height] and has values ranging from 0 to 1 (which is the fraction of the total screen size), so pick whatever values you want.
2 Comments
Image Analyst
on 11 Jun 2014
No it does not, but that doesn't matter. The figure will scale, but if the units of the other controls are all "characters" then they won't scale and will appear in the upper left corner. If you want them to scale/resize also, then set the units of all the controls to normalized. It looks like per's code should do that.
See Also
Categories
Find more on Migrate GUIDE 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!