Making GUI with sub-elements resizable in a quick way

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?

 Akzeptierte Antwort

per isakson
per isakson am 11 Jun. 2014
Bearbeitet: per isakson am 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 Kommentare

Matt J
Matt J am 19 Jun. 2014
Bearbeitet: Matt J am 19 Jun. 2014
That does work. Thanks again.
Unfortunately, whoever wrote this GUI formulated all the slider callbacks in hardcoded pixel units, or so it appears. Any movement of the sliders, once resized, makes things go crazy. :-(
per isakson
per isakson am 25 Jun. 2014
Bearbeitet: per isakson am 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

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (2)

per isakson
per isakson am 6 Jun. 2014
Bearbeitet: per isakson am 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 Kommentar

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!

Melden Sie sich an, um zu kommentieren.

Image Analyst
Image Analyst am 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 Kommentare

Are you saying that setting the 'Units' property of the parent figure automatically also changes the 'Units' property of all the uicontrol sub-elements that it contains? Or does that not matter?
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.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Creating, Deleting, and Querying Graphics Objects finden Sie in Hilfe-Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by