如何在 MATLAB 中使用 TeX/LaTeX 格式设置自定义数据提示?

发布于 2024-08-10 01:07:07 字数 579 浏览 6 评论 0原文

我正在尝试使用标有“R:...,Theta:...”的数据提示来注释极坐标图,其中 theta 实际上是希腊符号,而不是拼写出来的单词。我熟悉使用 '\theta' 生成符号的字符串格式,但在这种情况下它不起作用。有没有办法将 LaTeX 解释器应用于数据提示?这是我到目前为止所得到的:

f1=figure;
t=pi/4;
r=1;
polar(t,r,'.');
dcm_obj = datacursormode(f1);
set(dcm_obj,'UpdateFcn',@polarlabel)
info_struct = getCursorInfo(dcm_obj);
datacursormode on

其中 Polarlabel 定义如下:

function txt = polarlabel(empt,event_obj)
pos = get(event_obj,'Position');
x=pos(1);
y=pos(2);
[th,r]=cart2pol(x,y);
txt = {['R: ',num2str(r)],...
    ['\Theta: ',num2str(th*180/pi)]};

I'm trying to annotate a polar plot with data tips labelled with 'R:...,Theta:...' where theta is actually the Greek symbol, rather than the word spelled out. I'm familiar with string formatting using '\theta' resulting in the symbol, but it doesn't work in this case. Is there a way to apply the LaTeX interpreter to data tips? Here's what I have so far:

f1=figure;
t=pi/4;
r=1;
polar(t,r,'.');
dcm_obj = datacursormode(f1);
set(dcm_obj,'UpdateFcn',@polarlabel)
info_struct = getCursorInfo(dcm_obj);
datacursormode on

where polarlabel is defined as follows:

function txt = polarlabel(empt,event_obj)
pos = get(event_obj,'Position');
x=pos(1);
y=pos(2);
[th,r]=cart2pol(x,y);
txt = {['R: ',num2str(r)],...
    ['\Theta: ',num2str(th*180/pi)]};

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

掩于岁月 2024-08-17 01:07:07

更新:此解决方案主要适用于 R2014a 及更早版本,因为它对于较新版本似乎失败,特别是使用 新手柄图形系统。对于使用新手柄图形系统的较新版本,可以在此处找到解决方案。


由于某些奇怪的原因,MATLAB 中的 数据游标工具 强制设置数据提示文本按字面显示,而不是使用 TeX/LaTeX 解释(即使 默认 MATLAB 设置 说这样做)。似乎也无法通过数据游标模式对象属性直接设置文本属性。

不过,我已经找到了一种解决方法。如果将以下内容添加到 polarlabel 函数的末尾,文本应正确显示:

set(0,'ShowHiddenHandles','on');                       % Show hidden handles
hText = findobj('Type','text','Tag','DataTipMarker');  % Find the data tip text
set(0,'ShowHiddenHandles','off');                      % Hide handles again
set(hText,'Interpreter','tex');                        % Change the interpreter

解释

图中创建的每个图形对象都必须有一个 句柄。对象有时有其'HandleVisibility' property 设置为 'off',因此它们的句柄不会显示在其父对象的子对象列表中,从而使它们更难找到。解决此问题的一种方法是设置 'ShowHiddenHandles'根对象 属性 “开启”。然后,您将可以使用 findobj 查找具有某些属性的图形对象的句柄。 (注意:您还可以使用 findall 并且不用担心 'ShowHiddenHandles' 设置)

打开数据光标模式 并单击该图将创建一个 hggroup 对象,其中一个子对象是 文本object 用于显示的文本。上面的代码找到此文本对象并更改 '将 Interpreter' 属性 更改为 'tex',以便正确显示 theta 符号。

从技术上讲,上述代码只需调用一次,而不是每次调用 polarlabel 时。但是,直到您第一次单击绘图以显示数据提示(即第一次调用 polarlabel )时,文本对象才存在,因此代码必须放入 < code>UpdateFcn 用于数据游标模式对象,以便显示的第一个数据提示具有正确的文本格式。

Update: This solution is primarily applicable to versions R2014a and older, since it appears to fail for newer versions, specifically R2014b and newer using the new handle graphics system. For newer versions using the new handle graphics system, a solution can be found here.


For some odd reason, the data cursor tool in MATLAB forcibly sets the data tip text to be displayed literally instead of with TeX/LaTeX interpreting (even if the default MATLAB settings say to do so). There also appears to be no way of directly setting text properties via the data cursor mode object properties.

However, I've figured out one workaround. If you add the following to the end of your polarlabel function, the text should display properly:

set(0,'ShowHiddenHandles','on');                       % Show hidden handles
hText = findobj('Type','text','Tag','DataTipMarker');  % Find the data tip text
set(0,'ShowHiddenHandles','off');                      % Hide handles again
set(hText,'Interpreter','tex');                        % Change the interpreter

Explanation

Every graphics object created in the figure has to have a handle. Objects sometimes have their 'HandleVisibility' property set to 'off', so their handles won't show up in the list of child objects for their parent object, thus making them harder to find. One way around this is to set the 'ShowHiddenHandles' property of the root object to 'on'. This will then allow you to use findobj to find the handles of graphics objects with certain properties. (Note: You could also use findall and not worry about the 'ShowHiddenHandles' setting)

Turning on data cursor mode and clicking the plot creates an hggroup object, one child of which is the text object for the text that is displayed. The above code finds this text object and changes the 'Interpreter' property to 'tex' so that the theta symbol is correctly displayed.

Technically, the above code only has to be called once, not every time polarlabel is called. However, the text object doesn't exist until the first time you click on the plot to bring up the data tip (i.e. the first time polarlabel gets called), so the code has to go in the UpdateFcn for the data cursor mode object so that the first data tip displayed has the right text formatting.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文