This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

Why Has the Behavior of ResizeFcn Changed?

Changes in the behavior of the ResizeFcn callback reflect changes to the positioning and layout of figures and containers to make their behavior more consistent with one another and across all platforms.

ResizeFcn Returns Error After Program Launches

Starting in R2014b, the ResizeFcn callback might execute before all variables in the program file are assigned. When this happens, the ResizeFcn callback returns an error.

For example, this program has a ResizeFcn callback that uses a variable returned by the createGUI function.

function mygui
 
    hs = createGUI;
 
    function handles = createGUI
        % Create figure and its children
        f = figure('Tag','fig',...
                   'ResizeFcn',@doResizeFcn,...
                   'Visible','off');
        u = uicontrol('Parent',f,'Tag','ctrl');
        handles = guihandles(f);
        
        % make figure visible
        set(f,'Visible','on');
    end
 
    function doResizeFcn(varargin)
        length(hs)
    end
end
Running this program in the new graphics system results in an error because hs does not exist when the doResizeFcn callback executes for the first time (when the figure becomes visible).
mygui
Undefined function or variable "hs".
Error in mygui/doResizeFcn (line 18)
        length(hs)

Error using mygui/createGUI (line 14)
Error while evaluating Figure SizeChangedFcn

To correct the problem, make the figure visible after all the variables that the callback references are assigned. In this case, make the figure visible after calling the createGUI function.

function mygui
 
    hs = createGUI;
    % Make the figure visible
    set(hs.fig, 'Visible','on');
 
     function handles = createGUI
        % Create figure and its children
        f = figure('Tag','fig',...
                   'ResizeFcn',@doResizeFcn,...
                   'Visible','off');
        u = uicontrol('Parent',f,'Tag','ctrl');
        handles = guihandles(f);
 
    end
 
    function doResizeFcn(varargin)
        length(hs)
    end
end

ResizeFcn Inactive for Invisible Components

Starting in R2014b, changing the size of an invisible container, such as a figure, panel, button group, does not trigger the ResizeFcn callback until the container becomes visible.

In previous releases of MATLAB®, the ResizeFcn callback executes when the size of the container changes, regardless of whether it is visible.

You can control the visibility of figures and containers using the Visible property:

  • A figure is visible if its Visible property is set to 'on'.

  • A uipanel or uibuttongroup is visible if its Visible property, and that of its ancestors, is set to 'on'. For example, a uibuttongroup whose parent is a uipanel is visible when the 'Visible' property of the uibuttongroup, uipanel, and the figure are all set to 'on'.

Unexpected Behavior When Outer Bounds or Drawable Area Changes

Starting in R2014b, changing the outer bounds of a figure or container does not trigger the ResizeFcn callback.

For example, this figure ResizeFcn callback does not execute in the new graphics system when you change the OuterPosition (by removing the menu bar) on the second line of this code. However, the callback does execute in previous releases of MATLAB.

f = figure('ResizeFcn','display resized');
set(f,'Menubar','none');

Starting in R2014b, the ResizeFcn callback executes only when the container’s drawable area (the inner area) changes. Previous releases of MATLAB might not execute the ResizeFcn callback when the drawable area changes.

For example, this uipanel ResizeFcn callback executes in the new graphics system when you change the uipanel’s drawable area by increasing the border width. The callback does not execute when your run this code in previous releases.

p = uipanel('ResizeFcn','display resized');
set(p,'BorderWidth',3);

Related Topics