function setfigurepos(fig,pos) <fig> (optional) is the figure handle. can be a vector. default: [gcf]. <pos> is one of the following: (1) the position in normalized units (2) a scalar which indicates the scale factor to apply (anchoring the figure at the center) (3) the position in points units we decide that a case is (3) if any value is greater than 10. set the position of <fig> without mangling the 'Units' setting. example: figure; setfigurepos(1.5);
0001 function setfigurepos(fig,pos) 0002 0003 % function setfigurepos(fig,pos) 0004 % 0005 % <fig> (optional) is the figure handle. can be a vector. default: [gcf]. 0006 % <pos> is one of the following: 0007 % (1) the position in normalized units 0008 % (2) a scalar which indicates the scale factor to apply (anchoring the figure at the center) 0009 % (3) the position in points units 0010 % we decide that a case is (3) if any value is greater than 10. 0011 % 0012 % set the position of <fig> without mangling the 'Units' setting. 0013 % 0014 % example: 0015 % figure; setfigurepos(1.5); 0016 0017 % input 0018 if nargin==1 0019 pos = fig; 0020 fig = [gcf]; 0021 end 0022 0023 % do it 0024 for p=1:length(fig) 0025 0026 % store old 0027 prev = get(fig(p),'Units'); 0028 0029 % the scale factor case 0030 if length(pos)==1 0031 set(fig(p),'Units','normalized'); 0032 oldpos = getfigurepos(fig(p)); 0033 newsize = pos*oldpos(3:4); 0034 set(fig(p),'Position',[oldpos(1:2) - (newsize-oldpos(3:4))/2 newsize]); 0035 0036 % the points case 0037 elseif any(pos>10) 0038 set(fig(p),'Units','points'); 0039 set(fig(p),'Position',pos); 0040 0041 % the normalized case 0042 else 0043 set(fig(p),'Units','normalized'); 0044 set(fig(p),'Position',pos); 0045 end 0046 0047 % recall old 0048 set(fig(p),'Units',prev); 0049 0050 end