2

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

我曾经能够做到以下几点(根据“ gnovice - Amro ”方法):

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

并且会得到带有希腊字符的格式良好的数据提示标签。

但是,在新的 hg2 系统中,findall返回 a 0x0 empty GraphicsPlaceholder array,这使得设置Interpreter无用。

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

4

1 回答 1

1

在使用 进行一些挖掘之后uiinspect,我发现s"TextBox"现在存储为 's 属性中的matlab.graphics.shape.internal.GraphicsTip一种对象,而obj'sTipHandle属性又具有一个Interpreter属性!这两个属性都public可以使用点表示法轻松设置。我最终使用了以下代码:

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

笔记:

  • 函数 ( ) 的第一个输入obj不再被忽略,因为它现在有一些用处。
  • ishg2函数取自此 MATLAB Answer

编辑1:

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

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
于 2015-05-03T07:45:32.493 回答