matlabtextboxmatlab-figureundocumented-behaviormatlab-hg2

How to properly display TeX strings in axes' datatips? (MATLAB hg2)


I have recently tried to run an old piece of code (written on hg1) on a new version of MATLAB (2015a) that has hg2.

I used to be able to do the following (according to the "gnovice-Amro" method):

function output_txt = customDatatip(~,event_obj)
% Display the position of the data cursor
% obj          Currently not used (empty)
% event_obj    Handle to event object
% output_txt   Data cursor text string (string or cell array of strings).

hFig = ancestor(event_obj.Target,'figure'); %// I don't trust gcf ;)

pos = get(event_obj,'Position');
output_txt = {['\lambda: ',num2str(pos(1)*1000,4) 'nm'],...
              ['T(\lambda): ',num2str(pos(2),4) '%']};

set(findall(hFig, 'Type','text', 'Tag','DataTipMarker'),...
      'Interpreter','tex');     %// Change the interpreter

And would get nicely formatted datatip labels with Greek characters.

However, in the new hg2 system, findall returns a 0x0 empty GraphicsPlaceholder array, which renders setting the Interpreter useless.

My question is: How can I set the plot datatip interpreter to (La)TeX in hg2?


Solution

  • After some digging using uiinspect, I found that the "TextBox" is now stored as a matlab.graphics.shape.internal.GraphicsTip type of object within obj's TipHandle property which, in turn, has an Interpreter property! Both properties are public and can be set easily using dot notation. I've ended up using the following code:

    function output_txt = customDatatip(obj,event_obj)
    % Display the position of the data cursor // <- Autogenerated comment
    % obj          Currently not used (empty) // <- Autogenerated comment, NO LONGER TRUE!
    % event_obj    Handle to event object     // <- Autogenerated comment
    % output_txt   Data cursor text string (string or cell array of strings). // <- A.g.c.
    
    hFig = ancestor(event_obj.Target,'figure');
    
    pos = get(event_obj,'Position');
    output_txt = {['\lambda: ',num2str(pos(1)*1000,4) 'nm'],...
                  ['T(\lambda): ',num2str(pos(2),4) '%']};
    
    if ishg2(hFig)
        obj.TipHandle.Interpreter = 'tex';
    else %// The old version, to maintain backward compatibility:
            set(findall(hFig, 'Type','text', 'Tag','DataTipMarker'),...
                'Interpreter','tex');     % Change the interpreter
    end
    
    function tf = ishg2(fig)
    try
        tf = ~graphicsversion(fig, 'handlegraphics');
    catch
        tf = false;
    end
    

    Notes:

    Edit1:

    Just noticed that there's another way to check MATLAB's Graphic version (i.e. hg1/hg2) using the following code I found in the wavelet toolbox:

    function bool = isGraphicsVersion2
    %//isGraphicsVersion2 True for Graphic version 2. 
    
    %//   M. Misiti, Y. Misiti, G. Oppenheim, J.M. Poggi 21-Jun-2013.
    %//   Last Revision: 04-Jul-2013.
    %//   Copyright 1995-2013 The MathWorks, Inc.
    %//   $Revision: 1.1.6.1 $  $Date: 2013/08/23 23:45:07 $
    
    try
        bool = ~matlab.graphics.internal.isGraphicsVersion1;
    catch
        bool = ~isprop(0,'HideUndocumented');
    end