Question

Similar to: Setting graph figure size

But, I just want to set the width and height, without caring about the position. The desired behavior is that I can drag the figure around at will, but on each re-draw the size will be fixed.

I don't like the method in the above link because you must provide an (x,y) coordinate for the position, which is annoying as the code develops or I use different computers. But perhaps there is a smarter way to use that set() function?

EDIT: Cool @ answer below, here's my updated function. The other thing is to be "silent" so the figure doesn't pull focus constantly.

function h = sfigure(h,s1,s2)
% SFIGURE  Create figure window (minus annoying focus-theft).
%
% Usage is identical to figure.
%
% Daniel Eaton, 2005
%
% See also figure
%
% Modified by Peter Karasev, 2012, to optionally set scale
%

if nargin>=1 
    if ishandle(h)
        set(0, 'CurrentFigure', h);
    else
        h = figure(h);
    end
else
    h = figure;
end

if( nargin > 1 )
  scaleX = s1;
  scaleY = s1;
  if( nargin > 2 )
    scaleY = s2;
  end
  pos = get(h,'Position');
  pos(3:4) = [400 300].*[scaleX scaleY];
  set(gcf,'Position',pos);
end
Was it helpful?

Solution

Combine it with the corresponding get function:

figure
pos = get(gcf,'Position');
pos(3:4) = [w h];
set(gcf,'Position',pos);

This will keep the default position and only change the width and height.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top