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

前端 未结 1 1439
耶瑟儿~
耶瑟儿~ 2021-01-23 03:47

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 t

相关标签:
1条回答
  • 2021-01-23 04:01

    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:

    • The first input to the function (obj) is no longer ignored, since it has some use now.
    • The ishg2 function is taken from this MATLAB Answer.

    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
    
    0 讨论(0)
提交回复
热议问题