views:

299

answers:

1

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)]};
+5  A: 

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.

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.

gnovice
Thanks gnovice, this solution works perfectly! Excellent explanation as well.
Doresoom

related questions