View License

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

Highlights from
textborder - Higher contrast text using a 1-pixel-thick border

3.5 | 2 ratings Rate this file 14 Downloads (last 30 days) File Size: 1.73 KB File ID: #27383 Version: 1.2
image thumbnail

textborder - Higher contrast text using a 1-pixel-thick border


Joao Henriques (view profile)


27 Apr 2010 (Updated )

Draws text on a figure with a 1-pixel-thick border.

| Watch this File

File Information

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.

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.

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.)

imshow(imresize(imread('pears.png'), 0.5))
textborder(35, 215, 'Some text')
textborder(120, 100, 'Some text','w','k','FontSize',22)
textborder(270, 215, 'Some text',[0.7 0.8 1])


This file inspired Smith Chart With Real Time Data..

MATLAB release MATLAB 7.7 (R2008b)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (5)
02 Apr 2015 Lauren E

Can textborder handle 3-d plotting?

Comment only
24 Sep 2014 Sim

Sim (view profile)

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])

23 Oct 2013 Joao Henriques

Joao Henriques (view profile)

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?

Comment only
17 Oct 2013 Colin

Colin (view profile)

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');

Comment only
16 Oct 2013 Colin

Colin (view profile)

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:

textborder(30,30,'hello world')

28 Apr 2010 1.2

Changed the example and screenshot to more clearly demonstrate usage.

Contact us