我最近尝试在具有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
并且可以使用希腊字符获得格式良好的datatip标签。
但是,在新的hg2系统中,findall
会返回0x0 empty GraphicsPlaceholder array
,这会导致设置Interpreter
无用。
我的问题是:如何在hg2中将绘图数据提示解释器设置为(La)TeX?
答案 0 :(得分:1)
在使用uiinspect
进行一些挖掘之后,我发现"TextBox"
现在已存储为matlab.graphics.shape.internal.GraphicsTip
obj
属性中的TipHandle
类型的对象反过来,有一个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
功能取自this MATLAB Answer。注意到还有另一种方法可以使用我在小波工具箱中找到的以下代码检查MATLAB的Graphic版本(即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