如何在轴的数据提示中正确显示TeX字符串? (MATLAB hg2) [英] How to properly display TeX strings in axes' datatips? (MATLAB hg2)

查看:189
本文介绍了如何在轴的数据提示中正确显示TeX字符串? (MATLAB hg2)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近尝试在具有hg2的新版MATLAB(2015a)上运行一段旧代码(用hg1编写).

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

我曾经能够执行以下操作(根据" gnovice -Amro 方法):

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.

但是,在新的hg2系统中,findall返回0x0 empty GraphicsPlaceholder array,从而使Interpreter设置无效.

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

我的问题是:如何在hg2中将绘图数据提示解释器设置为(La)TeX?

推荐答案

使用现在作为matlab.graphics.shape.internal.GraphicsTip类型的对象存储在objTipHandle属性,而该属性又具有Interpreter属性!这两个属性均为public,可以使用点符号轻松设置.我最终使用了以下代码:

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

注意:

  • 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.

仅注意到,还有另一种方法可以使用在小波工具箱中找到的以下代码来检查MATLAB的图形版本(即hg1/hg2):

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

这篇关于如何在轴的数据提示中正确显示TeX字符串? (MATLAB hg2)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆