View License

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

» Watch video

Highlights from
textbp: text with legend-style "best" placement

4.5 | 11 ratings Rate this file 36 Downloads (last 30 days) File Size: 4.05 KB File ID: #11466 Version: 1.1
image thumbnail

textbp: text with legend-style "best" placement


Peter Mao (view profile)


19 Jun 2006 (Updated )

automatically locates text annotation to minimize figure obscuration

Editor's Notes:

This file was selected as MATLAB Central Pick of the Week

| Watch this File

File Information

Sometimes the exact location of some text annotation doesn't matter. This works just like TEXT (including the property calls) except one does not specify an X,Y location. The subroutine TSCAN (modified from the Matlab LSCAN routine) automatically places the text, with a preference for the bottom/left corner.

MATLAB release MATLAB 7.2 (R2006a)
Other requirements none
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (23)
25 May 2016 Lamya

Lamya (view profile)

04 May 2016 André Guerra

15 Feb 2016 Rodrigo Duran

and the error only happens sometimes, some 2 by 6 subplots worked just fine (each subplot uses textbp)

Comment only
14 Feb 2016 Rodrigo Duran

Very helpful function, however recently showing same error as Mathew Crema and Holger Hoffman have reported. 2 by 6 subplots

Undefined function or variable 'pop'.
Error in textbp>tscan (line 149)
minpop = min(min(pop));
Error in textbp (line 24)
pos = tscan(gca,width,height,TOL);

Comment only
22 Oct 2015 Richard Sims

There seems to be some issue if you use this in conjunction with reversing axis e.g set(gca,'YDir','reverse')

Comment only
13 Jun 2014 Holger Hoffmann

same issue as Matthew Crema
- no subplots
- the problem is "tscan"

"Undefined function or variable "pop".

Error in tscan (line 136)
minpop = min(min(pop));"

Comment only
06 Feb 2014 Matthew Crema

It does not work for me, and the error message is:

Undefined function or variable "pop".

I have a lot of subplots in one figure, which may make the task difficult.

Comment only
20 May 2013 Peter Mao

Peter Mao (view profile)

Ian -- legend tends to take the best upper-right position, while textbp looks for the best lower-left position. If your plot is fairly dense with data, they may decide on the same spot. textbp only pays attention to the data, it doesn't know where the legend is placed.

Alireza -- this is hard to debug without the error message. I'm going to guess that you put it somewhere where it's not in your path. Try "which textbp" or "path".

Comment only
17 May 2013 Alireza

It doesn't work for me but maybe I'm doing something wrong. I added the folder in the toolbox folder of Matlab and I'm using textbp('some string') after I plot some data. Is that right?

Comment only
09 May 2013 Ian

Ian (view profile)

This works well on its own, but it does not seem to play well with the legend command (MATLAB R2013a). Both legend and textbp choose the same overlapping 'best' spot.

Comment only
08 Sep 2012 Yi

Yi (view profile)

09 Jun 2011 Peter Mao

Peter Mao (view profile)

Jamal -- As I recall, that's related to a problem that occurs when translating between figure and axis normalized coordinates. I took it up w/ Mathworks years ago, with no satisfactory resolution, so there it stands.

Comment only
09 Jun 2011 Peter Mao

Peter Mao (view profile)

Adrien -- the trick is to rejigger TSCAN so it goes from top-left to bottom-right. I'll do that if I can find some time. It's been a long time since I looked at the code!

Comment only
28 Mar 2011 James

James (view profile)

I can't figure out how to use this code. Does this need to be placed in each .m file I wish to use the textbp function in?

Comment only
27 Jan 2011 Adrien

Adrien (view profile)

Problem : it does not seem to work with the function "hist". A solution ?

Comment only
09 Dec 2010 Faiz Mahdi

04 Nov 2010 Jamal Said

A problem: the repeated using of the function - in its simplest form - causes the overall added texts to be shifted diagonally.

15 Sep 2010 K E

K E (view profile)

Useful. Should be a Matlab builtin.

27 May 2010 Alonso

Alonso (view profile)

19 Apr 2010 chen

chen (view profile)

05 Jan 2009 Scott Foster

This is a great bit of code for what it does. Very easy to use, and exactly what I was looking for.

17 Oct 2006 Bob Solimeno

Beautiful! This code does exactly what I want to achieve. A very nice, simple substiture for the MATLAB text command where hands-off annotation is desired!

25 Jun 2006 Tak Kunihiro

This is a code that I was looking for. Since a command "legend" has similar functionality I even though that there might be a command already. I have been using "legend" to put all information in the plot, which was pain in the neck. Now I can make a plot with all experimental parameters.

21 Jun 2011 1.1

Modified TSCAN to properly avoid histogram bars.

Contact us