- Undocumented Matlab - https://undocumentedmatlab.com -

Undocumented HG2 graphics events

Posted By Yair Altman On May 27, 2015 | 18 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 [1]. 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 [2] 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

I prepared a utility called ticklabelformat that automates much of the set-up above. Feel free to download [3] 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

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

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 [4] 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);

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 [5], (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

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 [6]).
Additional graphics-related performance tips can be found in my Accelerating MATLAB Performance [7] 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.

Categories: Handle graphics, High risk of breaking in future versions, Listeners, Undocumented feature


18 Comments (Open | Close)

18 Comments To "Undocumented HG2 graphics events"

#1 Comment By Yaroslav On May 28, 2015 @ 01:21

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

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'

#2 Comment By Yair Altman On May 28, 2015 @ 01:25

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

#3 Comment By Yaroslav On May 28, 2015 @ 01:56

@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'));

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);

<Clicks on the axes>

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.

#4 Comment By Thomas On September 14, 2016 @ 10:52

Hey thanks, i will use ‘Hit’!

#5 Comment By Vlad Atanasiu On May 31, 2015 @ 07:18

Hello Yair,

You contributed to this discussion [14] on how to change event listening of such toolbar objects as zoom or pan. The solution consisted in using

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)

Thanks, Vlad

#6 Comment By Yair Altman On May 31, 2015 @ 07:55

@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 ( [15]). 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!

#7 Comment By Daniel On June 2, 2015 @ 02:42

While it seems like you can prevent re-entry with

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.

#8 Comment By Yair Altman On June 2, 2015 @ 03:53

@Daniel – I agree that an atomic (mutexed) [16] 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.

#9 Comment By Nick On November 2, 2016 @ 23:40

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

#10 Comment By Yair Altman On November 3, 2016 @ 08:57

@Nick –

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

#11 Comment By Nick On November 3, 2016 @ 15:25

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

Cheers!

#12 Comment By Nick On November 3, 2016 @ 18:48

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’

#13 Comment By Yair Altman On November 3, 2016 @ 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.

#14 Comment By Nick On November 3, 2016 @ 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

#15 Comment By Tucker Downs On May 17, 2021 @ 07:21

Hi Yair,

Is “markedClean” still your recommendation for wanting to redraw graphics primitive like objects? I’m working on trying to make some native-like graphics objects and need to know when to redraw.

Also, are you still looking for window transforms / data2pixel and its inverse? I have some info that you might find helpful. Will be posting code soon.

-Tucker

#16 Comment By Yair Altman On May 17, 2021 @ 23:15

@Tucker – AFAIK markedClean still works even in the very latest release. In recent years MathWorks added a few documented hooks for trapping axes updates etc., but the markedClean event still seems to be a general common mechanism.
If and when you have some information on the transforms, then please let me know.

#17 Comment By Adam D Danz On June 3, 2021 @ 19:44

Yair, thanks for your unmatched contributions to exploring Matlab under the hood.

I noticed that listeners responding to MarkedClean in geoaxes throw an error even though MarkedClean appears to have public ListenAccess.

ax = geoaxes('basemap','satellite');
MC = metaclass(ax);
E = MC.EventList;
E(10)

 GraphicsMetaEvent with properties:

    HasCallbackProperty: 0
                   Name: 'MarkedClean'
            Description: 'description'
    DetailedDescription: 'detailed description'
                 Hidden: 1
           NotifyAccess: 'public'
           ListenAccess: 'public'  <--------
          DefiningClass: [1×1 matlab.graphics.internal.GraphicsMetaClass]

But when assigning a listener to geoaxes,

addlistener(ax,'MarkedClean', ...
    'PostSet', @(a,b)myFunc(a,b));

Error using matlab.graphics.axis.GeographicAxes/addlistener
The name 'MarkedDirty' is not an accessible property for an instance of class 'matlab.graphics.axis.GeographicAxes'.

I’ve used this type of listener in other axes without problem but I don’t understand what’s blocking it with geoaxes.

#18 Comment By Adam D Danz On June 3, 2021 @ 20:57

Opps…. I was using the wrong syntax: MarkedClean is an EventName and I was accidentally treating it as a PropertyName. Also, ignore the “MarkedDirty” reference in the error message which was in response to a different version of the listener. #NewbieMistake

The first syntax below is the correct one to use with MarkedClean, as Yair demonstrated in this blog post.

el = addlistener(hSource, EventName, callback)  
el = addlistener(hSource, PropertyName, EventName, callback)

Article printed from Undocumented Matlab: https://undocumentedmatlab.com

URL to article: https://undocumentedmatlab.com/articles/undocumented-hg2-graphics-events

URLs in this post:

[1] series of articles about HG2: http://undocumentedmatlab.com/blog/tag/hg2

[2] setting axes tick labels format: http://undocumentedmatlab.com/blog/setting-axes-tick-labels-format

[3] download: http://www.mathworks.com/matlabcentral/fileexchange/36254-ticklabelformat

[4] axes ruler: http://undocumentedmatlab.com/blog/customizing-axes-rulers

[5] non-reentrant: http://undocumentedmatlab.com/blog/controlling-callback-re-entrancy

[6] more details: http://undocumentedmatlab.com/blog/property-value-change-listeners#comment-348826

[7] Accelerating MATLAB Performance: http://undocumentedmatlab.com/books/matlab-performance

[8] Capturing print events : https://undocumentedmatlab.com/articles/capturing-print-events

[9] Handle Graphics Behavior : https://undocumentedmatlab.com/articles/handle-graphics-behavior

[10] Matlab callbacks for Java events : https://undocumentedmatlab.com/articles/matlab-callbacks-for-java-events

[11] UDD Events and Listeners : https://undocumentedmatlab.com/articles/udd-events-and-listeners

[12] Waiting for asynchronous events : https://undocumentedmatlab.com/articles/waiting-for-asynchronous-events

[13] Waterloo graphics beta : https://undocumentedmatlab.com/articles/waterloo-graphics-beta

[14] : https://www.mathworks.com/matlabcentral/newsreader/view_thread/283529

[15] : https://undocumentedmatlab.com/blog/tag/hg2

[16] : https://en.wikipedia.org/wiki/Test-and-set

Copyright © Yair Altman - Undocumented Matlab. All rights reserved.