2

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
Community
  • 1
  • 1
peter karasev
  • 2,578
  • 1
  • 28
  • 38

1 Answers1

2

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.

tmpearce
  • 12,523
  • 4
  • 42
  • 60
  • cool, edited my question with this trick wrapped into my sfigure() function. – peter karasev Mar 10 '12 at 22:33
  • @peterkarasev Looks good... would be very cool if you could pass through the varargin list to `figure`, so its usage could be consistent with all the ways to normally create a figure. You could even check for the `Position` arg and allow that to override your custom implementation in case you (or other user) wants that functionality back. – tmpearce Mar 10 '12 at 22:40
  • Yeah that's a good point. But, the fixed position might break the "silent" nature of the figure; you want to be able to do other things anywhere on screen while something is running. Also, typically I have one or two figures that make sense for other users and another few that only make sense for me; no need to impose those on everyone's screen ;-) – peter karasev Mar 10 '12 at 23:06