MATLAB: Does the ‘extent’ property of text object return incorrect values in log-scaled axis

MATLAB

The 'extent' property of a text object returns an incorrect value in a log-scaled axis. Execute the following code to create a text object:
h = text(0.5,0.5,'Test Message');
Run the following code to get 'Extent' property, which gives a vector that defines the size and position of the text string:
extTxtLinear = get(h,'Extent')
Now set the axes to 'logscale' and get the 'Extent' property again:
set(gca,'yscale','log');
extTxtLogY = get(h,'Extent')
The 'extent' property returns the wrong value in log-scaled axis.

Best Answer

This is expected behavior in MATLAB 7.6 (R2008a) Graphics in the way that 'extent' property of text object returns gives incorrect values when the axis is set to log-scaled and units are data-specified.
The attached custom function EXTENTLOG can be used to determine correct 'x' and 'y' Extent values of a Text object in a log-scaled axis. This function takes the handle to a Text object as an input, and returns its 'x' and 'y' coordinates. It is not as advanced as MATLAB’s built in functionality, and does not output the width and height like the Extent property. However, it can be modified as needed.
Here is an example of EXTENTLOG’s usage:
%make a log-scaled axis with text
set(gca,'yscale','log');
set(gca,'xscale','log');
xlim([.01 100])
ylim([.3 32])
t = text(.2,.5,'Test Message','Units','normalized');
%call EXTENTLOG to calculate the text position
Answer = extentLog(t)
%plot the resulting Extent location
set(t,'Units','data')
hold on
plot(Answer(1),Answer(2),'o')
Related Question