setfigurepos.m 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. function setfigurepos(fig,pos)
  2. % function setfigurepos(fig,pos)
  3. %
  4. % <fig> (optional) is the figure handle. can be a vector. default: [gcf].
  5. % <pos> is one of the following:
  6. % (1) the position in normalized units
  7. % (2) a scalar which indicates the scale factor to apply (anchoring the figure at the center)
  8. % (3) the position in points units
  9. % we decide that a case is (3) if any value is greater than 10.
  10. %
  11. % set the position of <fig> without mangling the 'Units' setting.
  12. %
  13. % example:
  14. % figure; setfigurepos(1.5);
  15. % input
  16. if nargin==1
  17. pos = fig;
  18. fig = [gcf];
  19. end
  20. % do it
  21. for p=1:length(fig)
  22. % store old
  23. prev = get(fig(p),'Units');
  24. % the scale factor case
  25. if length(pos)==1
  26. set(fig(p),'Units','normalized');
  27. oldpos = getfigurepos(fig(p));
  28. newsize = pos*oldpos(3:4);
  29. set(fig(p),'Position',[oldpos(1:2) - (newsize-oldpos(3:4))/2 newsize]);
  30. % the points case
  31. elseif any(pos>10)
  32. set(fig(p),'Units','points');
  33. set(fig(p),'Position',pos);
  34. % the normalized case
  35. else
  36. set(fig(p),'Units','normalized');
  37. set(fig(p),'Position',pos);
  38. end
  39. % recall old
  40. set(fig(p),'Units',prev);
  41. end