function setpixelposition(h,position, isrecursive)
% SETPIXELPOSITION Set position HG object in pixels.

% ISRECURSIVE indicates whether the given POSITION is relative to the
% parent figure or to its parent. true means parent figure. 

% Copyright 1984-2004 The MathWorks, Inc.
% $Revision: 1.1.6.3 $ $Date: 2004/08/25 19:09:21 $

if nargin < 3
  isrecursive = false;
end

if isrecursive
    parent = get(h,'Parent');
    if ~isa(handle(parent),'figure')
        parentpos = getpixelposition(parent, true);
        position(1) = position(1) - parentpos(1);
        position(2) = position(2) - parentpos(2);        
    end
end

old_u = get(h,'Units');
set(h,'Units','pixels');
wasError = false;
try
    set(h,'Position',position);
catch
    wasError = true;
end
set(h,'Units',old_u);
if wasError
    rethrow(lasterror);
end

end
