Undocumented Matlab
  • SERVICES
    • Consulting
    • Development
    • Training
    • Gallery
    • Testimonials
  • PRODUCTS
    • IQML: IQFeed-Matlab connector
    • IB-Matlab: InteractiveBrokers-Matlab connector
    • EODML: EODHistoricalData-Matlab connector
    • Webinars
  • BOOKS
    • Secrets of MATLAB-Java Programming
    • Accelerating MATLAB Performance
    • MATLAB Succinctly
  • ARTICLES
  • ABOUT
    • Policies
  • CONTACT
  • SERVICES
    • Consulting
    • Development
    • Training
    • Gallery
    • Testimonials
  • PRODUCTS
    • IQML: IQFeed-Matlab connector
    • IB-Matlab: InteractiveBrokers-Matlab connector
    • EODML: EODHistoricalData-Matlab connector
    • Webinars
  • BOOKS
    • Secrets of MATLAB-Java Programming
    • Accelerating MATLAB Performance
    • MATLAB Succinctly
  • ARTICLES
  • ABOUT
    • Policies
  • CONTACT

Undocumented HG2 graphics events

May 27, 2015 14 Comments

R2014b brought a refreshing new graphics engine and appearance, internally called HG2 (the official marketing name is long and impossible to remember, and certainly not as catchy). I’ve already posted a series of articles about HG2. Today I wish to discuss an undocumented aspect of HG2 that I’ve encountered several times over the past months, and most recently today. The problem is that while in the previous HG1 system (R2014a and earlier) we could add property-change listener callbacks to practically any graphics object, this is no longer true for HG2. Many graphics properties, that are calculated on-the-fly based on other property values, cannot be listened-to, and so we cannot attach callbacks that trigger when their values change.

Property-change listeners in HG1

Take for example my post about setting axes tick labels format from 3 years ago: the idea there was to attach a Matlab callback function to the PropertyPostSet event of the XTick, YTick and/or ZTick properties, so that when they change their values (upon zoom/pan/resize), the corresponding tick-labels would be reformatted based on the user-specified format:

Formatted labels, automatically updated Formatted labels, automatically updated
Formatted labels, automatically updated


A simple HG1 usage might look as follows:

addlistener(handle(hAxes), 'YTick', 'PostSet', @reformatTickLabels);
function reformatTickLabels(hProperty, eventData)
    try
        hAxes = eventData.AffectedObject;
    catch
        hAxes = ancestor(eventData.Source,'Axes');
    end
    tickValues = get(hAxes, 'YTick');
    tickLabels = arrayfun(@(x)(sprintf('%.1fV',x)), tickValues, 'UniformOutput',false);
    set(hAxes, 'YTickLabel', tickLabels)
end

addlistener(handle(hAxes), 'YTick', 'PostSet', @reformatTickLabels); function reformatTickLabels(hProperty, eventData) try hAxes = eventData.AffectedObject; catch hAxes = ancestor(eventData.Source,'Axes'); end tickValues = get(hAxes, 'YTick'); tickLabels = arrayfun(@(x)(sprintf('%.1fV',x)), tickValues, 'UniformOutput',false); set(hAxes, 'YTickLabel', tickLabels) end

I prepared a utility called ticklabelformat that automates much of the set-up above. Feel free to download this utility from the Matlab File Exchange. Its usage syntax is as follows:

ticklabelformat(gca,'y','%.6g V')  % sets y axis on current axes to display 6 significant digits
ticklabelformat(gca,'xy','%.2f')   % sets x & y axes on current axes to display 2 decimal digits
ticklabelformat(gca,'z',@myCbFcn)  % sets a function to update the Z tick labels on current axes
ticklabelformat(gca,'z',{@myCbFcn,extraData})  % sets an update function as above, with extra data

ticklabelformat(gca,'y','%.6g V') % sets y axis on current axes to display 6 significant digits ticklabelformat(gca,'xy','%.2f') % sets x & y axes on current axes to display 2 decimal digits ticklabelformat(gca,'z',@myCbFcn) % sets a function to update the Z tick labels on current axes ticklabelformat(gca,'z',{@myCbFcn,extraData}) % sets an update function as above, with extra data

Property-change listeners in HG2

Unfortunately, this fails in HG2 when trying to listen to automatically-recalculated (non-Observable) properties such as the Position or axes Tick properties. We can only listen to non-calculated (Observable) properties such as Tag or YLim. Readers might think that this answers the need, since the ticks change when the axes limits change. This is true, but does not cover all cases. For example, when we resize/maximize the figure, Matlab may decide to modify the displayed ticks, although the axes limits remain unchanged.
So we need to have a way to monitor changes even in auto-calculated properties. Luckily this can be done by listening to a set of new undocumented HG2 events. It turns out that HG2’s axes (matlab.graphics.axis.Axes objects) have no less than 17 declared events, and 14 of them are hidden in R2015a:

>> events(gca)   % list the non-hidden axes events
Events for class matlab.graphics.axis.Axes:
    ObjectBeingDestroyed
    PropertyAdded
    PropertyRemoved
>> mc = metaclass(gca)
mc =
  GraphicsMetaClass with properties:
                     Name: 'matlab.graphics.axis.Axes'
              Description: 'TODO: Fill in Description'
      DetailedDescription: ''
                   Hidden: 0
                   Sealed: 1
                 Abstract: 0
              Enumeration: 0
          ConstructOnLoad: 1
         HandleCompatible: 1
          InferiorClasses: {0x1 cell}
        ContainingPackage: [1x1 meta.package]
             PropertyList: [414x1 meta.property]
               MethodList: [79x1 meta.method]
                EventList: [17x1 meta.event]
    EnumerationMemberList: [0x1 meta.EnumeratedValue]           SuperclassList: [7x1 meta.class]
>> mc.EventList(10)
ans =
  event with properties:
                   Name: 'MarkedClean'
            Description: 'description'
    DetailedDescription: 'detailed description'
                 Hidden: 1
           NotifyAccess: 'public'
           ListenAccess: 'public'
          DefiningClass: [1x1 matlab.graphics.internal.GraphicsMetaClass]
>> [{mc.EventList.Name}; ...
    {mc.EventList.ListenAccess}; ...
    arrayfun(@mat2str, [mc.EventList.Hidden], 'Uniform',false)]'
ans =
    'LocationChanged'             'public'       'true'
    'SizeChanged'                 'public'       'true'
    'ClaReset'                    'public'       'true'
    'ClaPreReset'                 'public'       'true'
    'Cla'                         'public'       'true'
    'ObjectBeingDestroyed'        'public'       'false'  % not hidden
    'Hit'                         'public'       'true'
    'LegendableObjectsUpdated'    'public'       'true'
    'MarkedDirty'                 'public'       'true'
    'MarkedClean'                 'public'       'true'
    'PreUpdate'                   'protected'    'true'
    'PostUpdate'                  'protected'    'true'
    'Error'                       'public'       'true'
    'Reparent'                    'public'       'true'
    'Reset'                       'public'       'true'
    'PropertyAdded'               'public'       'false'  % not hidden
    'PropertyRemoved'             'public'       'false'  % not hidden

>> events(gca) % list the non-hidden axes events Events for class matlab.graphics.axis.Axes: ObjectBeingDestroyed PropertyAdded PropertyRemoved >> mc = metaclass(gca) mc = GraphicsMetaClass with properties: Name: 'matlab.graphics.axis.Axes' Description: 'TODO: Fill in Description' DetailedDescription: '' Hidden: 0 Sealed: 1 Abstract: 0 Enumeration: 0 ConstructOnLoad: 1 HandleCompatible: 1 InferiorClasses: {0x1 cell} ContainingPackage: [1x1 meta.package] PropertyList: [414x1 meta.property] MethodList: [79x1 meta.method] EventList: [17x1 meta.event] EnumerationMemberList: [0x1 meta.EnumeratedValue] SuperclassList: [7x1 meta.class] >> mc.EventList(10) ans = event with properties: Name: 'MarkedClean' Description: 'description' DetailedDescription: 'detailed description' Hidden: 1 NotifyAccess: 'public' ListenAccess: 'public' DefiningClass: [1x1 matlab.graphics.internal.GraphicsMetaClass] >> [{mc.EventList.Name}; ... {mc.EventList.ListenAccess}; ... arrayfun(@mat2str, [mc.EventList.Hidden], 'Uniform',false)]' ans = 'LocationChanged' 'public' 'true' 'SizeChanged' 'public' 'true' 'ClaReset' 'public' 'true' 'ClaPreReset' 'public' 'true' 'Cla' 'public' 'true' 'ObjectBeingDestroyed' 'public' 'false' % not hidden 'Hit' 'public' 'true' 'LegendableObjectsUpdated' 'public' 'true' 'MarkedDirty' 'public' 'true' 'MarkedClean' 'public' 'true' 'PreUpdate' 'protected' 'true' 'PostUpdate' 'protected' 'true' 'Error' 'public' 'true' 'Reparent' 'public' 'true' 'Reset' 'public' 'true' 'PropertyAdded' 'public' 'false' % not hidden 'PropertyRemoved' 'public' 'false' % not hidden

Similar hidden events exist for all HG2 graphics objects. The MarkedDirty and MarkedClean events are available for practically all graphic objects. We can listen to them (luckily, their ListenAccess meta-property is defined as ‘public’) to get a notification whenever the corresponding object (axes, or any other graphics component such as a plot-line or axes ruler etc.) is being redrawn. We can then refresh our own properties. It makes sense to attach such callbacks to MarkedClean rather than MarkedDirty, because the property values are naturally stabled and reliable only after MarkedClean. In some specific cases, we might wish to listen to one of the other events, which luckily have meaningful names.
For example, in my ticklabelformat utility I’ve implemented the following code (simplified here for readability – download the utility to see the actual code), which listens to the MarkedClean event on the axes’ YRuler property:

try
    % HG1 (R2014a or older)
    hAx = handle(hAxes);
    hProp = findprop(hAx, 'YTick');
    hListener = handle.listener(hAx, hProp, 'PropertyPostSet', @reformatTickLabels);
    setappdata(hAxes, 'YTickListener', hListener);  % must be persisted in order to remain in effect
catch
    % HG2 (R2014b or newer)
    addlistener(hAx, 'YTick', 'PostSet', @reformatTickLabels);
    % *Tick properties don't trigger PostSet events when updated automatically in R2014b
    %addlistener(hAx, 'YLim', 'PostSet', @reformatTickLabels);  % this solution does not cover all use-cases
    addlistener(hAx.YRuler, 'MarkedClean', @reformatTickLabels);
end
% Adjust tick labels now
reformatTickLabels(hAxes);

try % HG1 (R2014a or older) hAx = handle(hAxes); hProp = findprop(hAx, 'YTick'); hListener = handle.listener(hAx, hProp, 'PropertyPostSet', @reformatTickLabels); setappdata(hAxes, 'YTickListener', hListener); % must be persisted in order to remain in effect catch % HG2 (R2014b or newer) addlistener(hAx, 'YTick', 'PostSet', @reformatTickLabels); % *Tick properties don't trigger PostSet events when updated automatically in R2014b %addlistener(hAx, 'YLim', 'PostSet', @reformatTickLabels); % this solution does not cover all use-cases addlistener(hAx.YRuler, 'MarkedClean', @reformatTickLabels); end % Adjust tick labels now reformatTickLabels(hAxes);

In some cases, the triggered event might pass some useful information in the eventData object that is passed to the callback function as the second input parameter. This data may be different for different events, and is also highly susceptible to changes across Matlab releases, so use with care. I believe that the event names themselves (MarkedClean etc.) are less susceptible to change across Matlab releases, but they might.

Performance aspects

The MarkedClean event is triggered numerous times, from obvious triggers such as calling drawnow to less-obvious triggers such as resizing the figure or modifying a plot-line’s properties. We therefore need to be very careful that our callback function is (1) non-reentrant, (2) is not active too often (e.g., more than 5 times per sec), (3) does not modify properties unnecessarily, and in general (4) executes as fast as possible. For example:

function reformatTickLabels(hProperty, eventData)
    persistent inCallback
    if ~isempty(inCallback),  return;  end
    inCallback = 1;  % prevent callback re-entry (not 100% fool-proof)
    % Update labels only every 0.2 secs or more
    persistent lastTime
    try
        tnow = datenummx(clock);  % fast
    catch
        tnow = now;  % slower
    end
    ONE_SEC = 1/24/60/60;
    if ~isempty(lastTime) && tnow - lastTime < 0.2*ONE_SEC
        inCallback = [];  % re-enable callback
        return;
    end
    lastTime = tnow;
    % This is the main callback logic
    try
        hAxes = eventData.AffectedObject;
    catch
        hAxes = ancestor(eventData.Source,'Axes');
    end
    prevTickValues = getappdata(hAxes, 'YTick');
    tickValues = get(hAxes, 'YTick');
    if ~isequal(prevTickValues, tickValues)
        tickLabels = arrayfun(@(x)(sprintf('%.1fV',x)), tickValues, 'UniformOutput',false);
        set(hAxes, 'YTickLabel', tickLabels)
    end
    inCallback = [];  % re-enable callback
end

function reformatTickLabels(hProperty, eventData) persistent inCallback if ~isempty(inCallback), return; end inCallback = 1; % prevent callback re-entry (not 100% fool-proof) % Update labels only every 0.2 secs or more persistent lastTime try tnow = datenummx(clock); % fast catch tnow = now; % slower end ONE_SEC = 1/24/60/60; if ~isempty(lastTime) && tnow - lastTime < 0.2*ONE_SEC inCallback = []; % re-enable callback return; end lastTime = tnow; % This is the main callback logic try hAxes = eventData.AffectedObject; catch hAxes = ancestor(eventData.Source,'Axes'); end prevTickValues = getappdata(hAxes, 'YTick'); tickValues = get(hAxes, 'YTick'); if ~isequal(prevTickValues, tickValues) tickLabels = arrayfun(@(x)(sprintf('%.1fV',x)), tickValues, 'UniformOutput',false); set(hAxes, 'YTickLabel', tickLabels) end inCallback = []; % re-enable callback end

Unfortunately, it seems that retrieving some property values (such as the axes’s YTick values) may by itself trigger the MarkedClean event for some reason that eludes my understanding (why should merely getting the existing values modify the graphics in any way?). Adding callback re-entrancy checks as above might alleviate the pain of such recursive callback invocations.
A related performance aspect is that it could be better to listen to a sub-component’s MarkedClean than to the parent axes’ MarkedClean, which might be triggered more often, for changes that are entirely unrelated to the sub-component that we wish to monitor. For example, if we only monitor YRuler, then it makes no sense to listen to the parent axes’ MarkedClean event that might trigger due to a change in the XRuler.
In some cases, it may be better to listen to specific events rather than the all-encompassing MarkedClean. For example, if we are only concerned about changes to the Position property, we should listen to the LocationChanged and/or SizeChanged events (more details).
Additional graphics-related performance tips can be found in my Accelerating MATLAB Performance book.
Have you used MarkedClean or some other undocumented HG2 event in your code for some nice effect? If so, please share your experience in a comment below.

Related posts:

  1. Capturing print events – Matlab print events can be trapped by users to enable easy printout customization. ...
  2. Handle Graphics Behavior – HG behaviors are an important aspect of Matlab graphics that enable custom control of handle functionality. ...
  3. Matlab callbacks for Java events – Events raised in Java code can be caught and handled in Matlab callback functions - this article explains how...
  4. UDD Events and Listeners – UDD event listeners can be used to listen to property value changes and other important events of Matlab objects...
  5. Waiting for asynchronous events – The Matlab waitfor function can be used to wait for asynchronous Java/ActiveX events, as well as with timeouts. ...
  6. Waterloo graphics beta – The Waterloo graphics library extends Matlab graphics with numerous customizable plots that can be embedded in Matlab figures. ...
Callbacks Handle graphics HG2 Listener Pure Matlab
Print Print
« Previous
Next »
14 Responses
  1. Yaroslav May 28, 2015 at 01:21 Reply

    The HG2 axes events became hidden in version R2015a. In my R2014b they are visible:

    >> events(gca)
    Events for class matlab.graphics.axis.Axes:
        LocationChanged
        SizeChanged
        ClaReset
        ClaPreReset
        Cla
        ObjectBeingDestroyed
        Hit
        LegendableObjectsUpdated
        MarkedDirty
        MarkedClean
        Error
        Reparent
        Reset
        PropertyAdded
        PropertyRemoved

    >> events(gca) Events for class matlab.graphics.axis.Axes: LocationChanged SizeChanged ClaReset ClaPreReset Cla ObjectBeingDestroyed Hit LegendableObjectsUpdated MarkedDirty MarkedClean Error Reparent Reset PropertyAdded PropertyRemoved

    I recall making use of the Hit event on several occasions. IMHO, it is the most useful event in the list. Can’t see why TMW decided to hide it in the latest release.

    The only hidden events in R2014b are PreUpdate and PostUpdate, which have a protected Observable value:

    >> mc = metaclass(gca);
    >> [{mc.EventList.Name}; ...
        {mc.EventList.ListenAccess}; ...
        arrayfun(@mat2str, [mc.EventList.Hidden], 'Uniform',false)]'
    ans = 
        'LocationChanged'             'public'       'false'
        'SizeChanged'                 'public'       'false'
        'ClaReset'                    'public'       'false'
        'ClaPreReset'                 'public'       'false'
        'Cla'                         'public'       'false'
        'ObjectBeingDestroyed'        'public'       'false'
        'Hit'                         'public'       'false'
        'LegendableObjectsUpdated'    'public'       'false'
        'MarkedDirty'                 'public'       'false'
        'MarkedClean'                 'public'       'false'
        'PreUpdate'                   'protected'    'false'
        'PostUpdate'                  'protected'    'false'
        'Error'                       'public'       'false'
        'Reparent'                    'public'       'false'
        'Reset'                       'public'       'false'
        'PropertyAdded'               'public'       'false'
        'PropertyRemoved'             'public'       'false'

    >> mc = metaclass(gca); >> [{mc.EventList.Name}; ... {mc.EventList.ListenAccess}; ... arrayfun(@mat2str, [mc.EventList.Hidden], 'Uniform',false)]' ans = 'LocationChanged' 'public' 'false' 'SizeChanged' 'public' 'false' 'ClaReset' 'public' 'false' 'ClaPreReset' 'public' 'false' 'Cla' 'public' 'false' 'ObjectBeingDestroyed' 'public' 'false' 'Hit' 'public' 'false' 'LegendableObjectsUpdated' 'public' 'false' 'MarkedDirty' 'public' 'false' 'MarkedClean' 'public' 'false' 'PreUpdate' 'protected' 'false' 'PostUpdate' 'protected' 'false' 'Error' 'public' 'false' 'Reparent' 'public' 'false' 'Reset' 'public' 'false' 'PropertyAdded' 'public' 'false' 'PropertyRemoved' 'public' 'false'

    • Yair Altman May 28, 2015 at 01:25 Reply

      @Yaroslav – thanks. It would be useful to hear how and why you’ve used the Hit event.

    • Yaroslav May 28, 2015 at 01:56 Reply

      @Yair — let’s examine a minimal working example:

      ax=axes('NextPlot','add');
      addlistener(ax,'Hit',@(obj,evd)plot(evd.IntersectionPoint(1),evd.IntersectionPoint(2),'ob'));

      ax=axes('NextPlot','add'); addlistener(ax,'Hit',@(obj,evd)plot(evd.IntersectionPoint(1),evd.IntersectionPoint(2),'ob'));

      It plots a point on the axes each time it is hit. One could argue that the ButtonDownFcn property does the same thing. Yet, upon closer inspection, under the hood ButtonDownFcn uses Hit to operate:

      ax.ButtonDownFcn = @(obj,evd)display(evd);

      ax.ButtonDownFcn = @(obj,evd)display(evd);

      <Clicks on the axes>

      evd = 
       
        Hit with properties:
       
                     Button: 1
          IntersectionPoint: [0.4223 0.3771 0]
                     Source: [1x1 Axes]
                  EventName: 'Hit'

      evd = Hit with properties: Button: 1 IntersectionPoint: [0.4223 0.3771 0] Source: [1x1 Axes] EventName: 'Hit'

      If one needs only a single callback, I guess that ButtonDownFcn will suffice. If there is a necessity for more than one, Hit is invaluable.

    • Thomas September 14, 2016 at 10:52 Reply

      Hey thanks, i will use ‘Hit’!

  2. Vlad Atanasiu May 31, 2015 at 07:18 Reply

    Hello Yair,

    You contributed to this discussion https://www.mathworks.com/matlabcentral/newsreader/view_thread/283529 on how to change event listening of such toolbar objects as zoom or pan. The solution consisted in using

    set(hManager.WindowListenerHandles,'Enable','off');

    set(hManager.WindowListenerHandles,'Enable','off');

    FYI it appears however that in R2014b modifying WindowListenerHandles is not possible: its SetAccess property is set to protected. Additionally, the former property Enable, a string ( ‘on’ | ‘off’ ), is now called Enabled and has become a scalar ( 0|1 ). Try:

    hManager = uigetmodemanager(gcf);
    mc = metaclass(hManager)
    mc.PropertyList(5)

    hManager = uigetmodemanager(gcf); mc = metaclass(hManager) mc.PropertyList(5)

    Thanks, Vlad

    • Yair Altman May 31, 2015 at 07:55 Reply

      @Vlad – that post is 5 years old and used undocumented/unsupported functionality. What’s surprising is not that it finally broke in R2014b, but that it kept working for so long. You can’t expect undocumented/unsupported functionality to keep working forever.

      Specifically, R2014b broke quite a lot of undocumented features in its change of the graphics system (HG2). In most cases, things that broke have similarly-undocumented (or documented) equivalents in HG2, you just need to dig around a bit to discover them. The hard part was discovering the initial undocumented features in HG1; finding their equivalents in HG2 should be much easier.

      Good luck!

  3. Daniel June 2, 2015 at 02:42 Reply

    While it seems like you can prevent re-entry with

    persistent inCallback
    if ~isempty(inCallback),  return;  end
    inCallback = 1;  % disable callback re-entrancy

    persistent inCallback if ~isempty(inCallback), return; end inCallback = 1; % disable callback re-entrancy

    since MATLAB timers run asynchronously and can execute between any two lines of code, if a timer callback occurs between the check and setting of inCallback, then you can get re-entry if the timer callback either calls reformatTickLabels or flushes the event queue and reformatTickLabels is in the event queue. I think to be safe you would need to do the check and setting of inCallback atomically, possibly with a mex file.

    • Yair Altman June 2, 2015 at 03:53 Reply

      @Daniel – I agree that an atomic (mutexed) test-and-set function would be better, although in practice the probability of reentrancy with the above code is low. If you create such a MEX-based function and post it on the Matlab File Exchange , I’ll be happy to reference it in this blog.

  4. Nick November 2, 2016 at 23:40 Reply

    I’ve been trying to find a way to prevent our users from deleting axes when they are deleting annotations. (This happens way more than I expected, and it is a major source of frustration). I thought it’d be easy to find an event or callback to hook into and pop up a dialog, but it’s proven fairly difficult for me to crack.

    I thought maybe I could set a listener for ObjectBeingDestroyed, but it fires after the object is gone (or there’s a synch issue).

    Is there some kind of ObjectAboutToBeDestroyed or DestroyObjectRequest thats double-secret-undocumented?

    Any thoughts as to how I can hook into this behavior (or as a welcomed alternative, turn off the ability to delete axes with the keyboard – I mean seriously, why is that even a thing?)

    function testAxesDelete
        hs = struct('fig', figure);
        hs.fig.MenuBar = 'none';
        hs.fig.ToolBar = 'figure';
        hs.fig.KeyPressFcn = @myKeyCallback;
     
        hs.ax = axes;
        hs.line = plot([0 0], [-1 1]);
     
        hs.ax.DeleteFcn = @myDeleteCallback;
        hs.ax.ButtonDownFcn = @myButtonCallback;
     
        addlistener(hs.ax, 'ObjectBeingDestroyed', @obdCallback);    
    end
     
    function myKeyCallback(hobj, event, varargin)
        disp('Pressed a key on figure!');
        disp('Axes still is deleted if you press delete with the axis selected');
        disp('Axis is deleted prior to executing this function')
        keyboard
    end
     
    function myDeleteCallback(hobj, event, varargin)
        disp('Deleting axes!');
        disp('Axis is deleted prior to executing this function')
        keyboard
    end
     
    function myButtonCallback(hobj, event, varargin)
        disp('Does not intercept delete key');
        keyboard
    end
     
    function obdCallback(hobj, event, varargin)
        disp('Object being destroyed callback');
        disp('this happens *after* the axes have been deleted ');
        keyboard
    end

    function testAxesDelete hs = struct('fig', figure); hs.fig.MenuBar = 'none'; hs.fig.ToolBar = 'figure'; hs.fig.KeyPressFcn = @myKeyCallback; hs.ax = axes; hs.line = plot([0 0], [-1 1]); hs.ax.DeleteFcn = @myDeleteCallback; hs.ax.ButtonDownFcn = @myButtonCallback; addlistener(hs.ax, 'ObjectBeingDestroyed', @obdCallback); end function myKeyCallback(hobj, event, varargin) disp('Pressed a key on figure!'); disp('Axes still is deleted if you press delete with the axis selected'); disp('Axis is deleted prior to executing this function') keyboard end function myDeleteCallback(hobj, event, varargin) disp('Deleting axes!'); disp('Axis is deleted prior to executing this function') keyboard end function myButtonCallback(hobj, event, varargin) disp('Does not intercept delete key'); keyboard end function obdCallback(hobj, event, varargin) disp('Object being destroyed callback'); disp('this happens *after* the axes have been deleted '); keyboard end

    • Yair Altman November 3, 2016 at 08:57 Reply

      @Nick –

      set(gca,'HitTest','off')

      set(gca,'HitTest','off')

    • Nick November 3, 2016 at 15:25 Reply

      You are my hero – thank you for this invaluable resource. And thank you for pointing me to an (embarrassed) *documented* feature.

      Cheers!

    • Nick November 3, 2016 at 18:48 Reply

      I just learned a fun fact: plotting to an axes object who’s HitTest property is set to ‘off’ appears to reset HitTest to ‘on’

      • Yair Altman November 3, 2016 at 19:07

        @Nick – this is a “known” undocumented side-effect of using the high-level plotting functions (such as plot), which reset several axes properties including HitTest and Tag. In contrast, the low-level plotting functions (such as line) do not reset these properties.

      • Nick November 3, 2016 at 20:05

        Yair – It looks like setting the Axes ‘NextPlot’ property to ‘add’ may prevent the resetting of HitTest. There is more behavior to investigate, but at least I seem to be on some kind of path. Thank you again

Leave a Reply
HTML tags such as <b> or <i> are accepted.
Wrap code fragments inside <pre lang="matlab"> tags, like this:
<pre lang="matlab">
a = magic(3);
disp(sum(a))
</pre>
I reserve the right to edit/delete comments (read the site policies).
Not all comments will be answered. You can always email me (altmany at gmail) for private consulting.

Click here to cancel reply.

Useful links
  •  Email Yair Altman
  •  Subscribe to new posts (email)
  •  Subscribe to new posts (feed)
  •  Subscribe to new posts (reader)
  •  Subscribe to comments (feed)
 
Accelerating MATLAB Performance book
Recent Posts

Speeding-up builtin Matlab functions – part 3

Improving graphics interactivity

Interesting Matlab puzzle – analysis

Interesting Matlab puzzle

Undocumented plot marker types

Matlab toolstrip – part 9 (popup figures)

Matlab toolstrip – part 8 (galleries)

Matlab toolstrip – part 7 (selection controls)

Matlab toolstrip – part 6 (complex controls)

Matlab toolstrip – part 5 (icons)

Matlab toolstrip – part 4 (control customization)

Reverting axes controls in figure toolbar

Matlab toolstrip – part 3 (basic customization)

Matlab toolstrip – part 2 (ToolGroup App)

Matlab toolstrip – part 1

Categories
  • Desktop (45)
  • Figure window (59)
  • Guest bloggers (65)
  • GUI (165)
  • Handle graphics (84)
  • Hidden property (42)
  • Icons (15)
  • Java (174)
  • Listeners (22)
  • Memory (16)
  • Mex (13)
  • Presumed future risk (394)
    • High risk of breaking in future versions (100)
    • Low risk of breaking in future versions (160)
    • Medium risk of breaking in future versions (136)
  • Public presentation (6)
  • Semi-documented feature (10)
  • Semi-documented function (35)
  • Stock Matlab function (140)
  • Toolbox (10)
  • UI controls (52)
  • Uncategorized (13)
  • Undocumented feature (217)
  • Undocumented function (37)
Tags
ActiveX (6) AppDesigner (9) Callbacks (31) Compiler (10) Desktop (38) Donn Shull (10) Editor (8) Figure (19) FindJObj (27) GUI (141) GUIDE (8) Handle graphics (78) HG2 (34) Hidden property (51) HTML (26) Icons (9) Internal component (39) Java (178) JavaFrame (20) JIDE (19) JMI (8) Listener (17) Malcolm Lidierth (8) MCOS (11) Memory (13) Menubar (9) Mex (14) Optical illusion (11) Performance (78) Profiler (9) Pure Matlab (187) schema (7) schema.class (8) schema.prop (18) Semi-documented feature (6) Semi-documented function (33) Toolbar (14) Toolstrip (13) uicontrol (37) uifigure (8) UIInspect (12) uitools (20) Undocumented feature (187) Undocumented function (37) Undocumented property (20)
Recent Comments
  • Patrick Fitzgerald (1 day 23 hours ago): Just a heads up to anyone else digging around for help related to this: if you assign BackrgoundColor to a pushbutton-style uicontrol, it seems to prevent you from...
  • Vasiliy (17 days 19 hours ago): Hi Yair, i’m trying to use the MonthChooserPanel class. com.mathworks.mwswing.MJUtilit ies.initJIDE; handles.jPanel = com.jidesoft.combobox.MonthCho oserPanel;...
  • DM (17 days 19 hours ago): Hi Yair, I’m trying to use the MonthChooserPanel class. com.mathworks.mwswing.MJUtilit ies.initJIDE; handles.jPanel = com.jidesoft.combobox.MonthCho oserPanel; [handles.hPanel,...
  • Yair Altman (21 days 11 hours ago): @Alex – thanks, but keep in mind that new functions will only work on the recent Matlab releases. If your code needs to work on older Matlab releases, you could revert to...
  • Alex Churchill (21 days 12 hours ago): I was looking up how to do this exact task today. I was about to hold my nose and use the internal controllib function, when I happened to chance across a slightly newer...
  • Sebastian Hölz (27 days 18 hours ago): I have not investigated this in detail, but I think one way to go in new versions of Matlab (>2019b) might be to use listeners to suitable figure properties, e.g. fig =...
  • Prathep (27 days 21 hours ago): Hi Yair, Thanks for your introduction on Matlab Toostrip. Is there any way to add Matlab toolstrip for uifigures as you have done already for figure?
  • Josh (34 days 3 hours ago): Dear Yair, Small typo; you wrote >>Move lines up or down – CTRL + ALT + UP or DOWN allows you to move selected lines up or down but it’s actually ALT+SHIFT then UP/DOWN...
  • Yair Altman (40 days 22 hours ago): You can try to increase the Java heap memory size in the Matlab preferences (General => Java Heap Memory). Any changes to the settings will only take effect after restarting...
  • Thomas (40 days 23 hours ago): Hello, thanks for sharing! I currently receive the following memory error message when using savezip with a big object (Matlab R2020b, Windows 10). Error using...
  • Yair Altman (44 days 7 hours ago): Yerlan – either use menuItem1.setEnabled(0) or set(menuItem1,'Enabled',0)
  • Manzn (44 days 9 hours ago): Thank you man! you saved me, when there was no more light 😀
  • Yerlan (45 days 14 hours ago): Hello Mr. Altman, how can I disable menu items in the context menu? E.g. when I am trying to do this: menuItems = jmenu.getSubElements; menuItem1 = menuItems(1);...
  • Yair Altman (46 days 6 hours ago): Thanks for the note Eric – you forgot the crucial call to jTable.setLabelTable(labelTabl e) – I added it into your code snippet above.
  • Erik (47 days 18 hours ago): Thank you for this — I don’t know if this has been mentioned anywhere else before, but it could be useful to mention how to add custom labels to a jslider. I did the...
Contact us
Undocumented Matlab © 2009 - Yair Altman
Scroll to top