File Exchange

image thumbnail


version (5.45 KB) by Jerome Briot
Two flexible wrappers to manage graphics object position.


Updated 21 Feb 2007

No License

Editor's Note: This was a File Exchange Pick of the Week

SETPOS/GETPOS are two flexible wrappers to set/get the "Position" property of graphics objects (figure, axes, uicontrol).

SETPOS(H,POS) works as SET(H,'Position',POS) and GETPOS(H) works as GET(H,'Position') but :

Both can set/get data using different unit for each element of the 'Position' vector.

Both can manage each element [Left,Bottom,Width,Height] individually without the others.

Both can use any graphics object HREF as position reference, instead of the parent of H. (Added in V1.1)

SETPOS contains a +/- feature that increase/decrease the corresponding value in the 'Position' vector.

Please contact me directly by email for bug report or enhancement request.


(Both) 1.0 (12-Feb-2007)

(Both) 1.1 (14-Feb-2007)
Third input argument HREF added.
Minor corrections in the help section.

(Both) 1.2 (21-Feb-2007)
Bug fixed if HREF is the Root object.
Examples removed from the help section.
Third file containing examples added.

Cite As

Jerome Briot (2021). SETPOS 1.2 - GETPOS 1.2 (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (7)

Dan K

Naor: I've made it work with some minor tweaks, in both cases near line 159. In SetPos:

% Get the current "Position" vector of H
% Test for root:
if verLessThan('matlab','8.4')
rootTest = ~href;
rootTest = isequal(href,groot);
% Get the current "Position" vector of HREF
if rootTest % HREF is the Root object (no 'Position' property)
position_ref_unit=get(href,'screensize'); %%% Should be safe here !
else position_ref_unit=get(href,'position');
% if any(~flag) % No "+" or "-"
if (~flag(n)) % No "+" or "-"

In GetPos:

% Get the current "Position" vector of HREF
% Test for root:
if verLessThan('matlab','8.4')
rootTest = ~href;
rootTest = isequal(href,groot);
if rootTest % HREF is the Root object (no 'Position' property)

Naor Movshovitz

Really nice utility for MATLAB releases up to 2014a. Hopefully an update that works in HG2 (R2014b+) is in the works?


(This comment and the one following both apply to the getpos function only.)

In the fragment:

% Don't use HREF position if it is the parent of H
if href~=get(h,'parent')

the line "href=h;" appears erroneous. After removal, the function works as I expected it to.


Very nicely done. Unfortunately I think there's an issue when finding positions relative to reference object. The line:

pos(n) = temp(n) - temp_href(n) * flag_href;

is correct for n = 1 or 2 (left & bottom elements of the position vector), but doesn't seem to make sense for n = 3 or 4 (width and height). Stating an object's position relative to a non-parent reference object should only affect location, not size, when working with absolute units (or "nz" if we assume everything's normalized to the same figure). And if the intent was to return object height & width relative to a reference object (for normalized units), the above line isn't the correct way to find them.

Jos x@y.z

I have to take back my comment about the naming conflict: setpos is a private function of the SP toolbox.

Jos x@y.z

Quite usefull. Well done. Good help, albeit what long.
There is a possible name class with setpos in the signal toolbox.

Peter (PB) Bodin

Very useful utility. H1-line, error checking, cleanly written and well commented code. Well done.

MATLAB Release Compatibility
Created with R12.1
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

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

Start Hunting!