This function draws text on a figure with a 1-pixel-thick border, which may be useful when simple text would be hard to read due to low contrast.
TEXTBORDER(X, Y, STRING)
Creates text on the current figure with a one-pixel border around it. The default colors are white text on a black border, which provides high contrast in most situations.
TEXTBORDER(X, Y, STRING, TEXT_COLOR, BORDER_COLOR)
Optional TEXT_COLOR and BORDER_COLOR specify the colors to be used.
Optional properties for the native TEXT function (such as 'FontSize') can be supplied after all the other parameters.
Since usually the units of the parent axes are not pixels, resizing it may subtly change the border of the text out of position. Either set the right size for the figure before calling TEXTBORDER, or always redraw the figure after resizing it.
The screenshot was created using the following code. (The image is part of the Matlab Image Processing Toolbox demos.)
textborder(35, 215, 'Some text')
textborder(120, 100, 'Some text','w','k','FontSize',22)
textborder(270, 215, 'Some text',[0.7 0.8 1])
Nice script! If you need to use it on a map, substitute "textm" for "text."
Can textborder handle 3-d plotting?
Works really nice. There a bug when using graphics with transparency (when the figure's render property is set to 'opengl'). It might be the same bug as Colin suffered. I fixed it by forcing the white text with an offset of 1 in its z coordinate, while the black text remains on the z=0 plane.
line 44 of the April 2010 version:
set(h, 'Position', [pos(1:2), 1])
Hi Colin, I tried your code on R2012a but can't reproduce the bug.
I also developed the code on R2008b and used it with "surface" objects, of which "pcolor" is one example, and had no problems as well.
It must be a MATLAB bug in the specific version you're using. Which one is it?
If anyone else has this trouble, here's the reason: When the function converts back to data units for the position of the offset text, for some reason Matlab sets the z value to 17.3205 (in my bug example). It doesn't do that when the entered values don't change the x and y positions, such as for the centered white text... My work around is to add a couple lines in the offset for-loop setting the z back to 0 after the unit conversion, like this:
pos = get(h(k),'Position');
This function works on a regular figure, but not if you are adding annotation over a pcolor plot. For some reason the four border text objects plot over the white centered one.
I've unsuccessfully tried the following modifications to the function:
1) adding an extra white centered one plotted first so that the black ones should be sandwhiched.
2) using uistack to explicitly try and tell Matlab which one to put on top.
Reversing the colors does not help. Something about the offsetting over a pcolor plot makes matlab force that text to stay on top in the plotting stack.
So unfortunately it's no use in the most likely situation I would want to add contrast to text.
Example of the bug:
Changed the example and screenshot to more clearly demonstrate usage.