Graphics |
Creating Text Annotations with the text or gtext Command
To create a text annotation using the text
function, you must specify the text and its location in the graph, using x- and y-coordinates. You specify the coordinates in the units of the graph.
Use the gtext
command when you want to position a text annotation at a specific point in the data space with the mouse.
For example, the following code creates text annotations at specific points in the Lotka-Volterra Predator-Prey Population Model graph.
str1(1) = {'Many Predators;'}; str1(2) = {'Prey Population'}; str1(3) = {'Will Decline'}; text(7,220,str1) str2(1) = {'Few Predators;'}; str2(2) = {'Prey Population'}; str2(3) = {'Will Increase'}; text(5.5,125,str2)
This example also illustrates how to create multiline text annotations with cell arrays.
Calculating the Positions of Text Annotations
You can also calculate the positions of text annotations in a graph. The following code adds annotations at three data points on a graph.
text(3*pi/4,sin(3*pi/4),...
'\leftarrowsin(t) = .707',...
'FontSize',16)
text(pi,sin(pi),'\leftarrowsin(t) = 0',... 'FontSize',16) text(5*pi/4,sin(5*pi/4),'sin(t) = -.707\rightarrow',... 'HorizontalAlignment','right',... 'FontSize',16)
The HorizontalAlignment of the text string 'sin(t) =
-.707 \rightarrow'
is set to right
to place it on the left side of the point [5*pi/4,sin(5*pi/4)]
on the graph. For more information about aligning text annotations, see Text Alignment.
Defining Symbols. For information on using symbols in text strings, see Mathematical Symbols, Greek Letters, and TEX Characters.
You can use text objects to annotate axes at arbitrary locations. MATLAB locates text in the data units of the axes. For example, suppose you plot the function with A = 0.25, = 0.005, and t = 0 to 900.
To annotate the point where the value of t = 300, calculate the text coordinates using the function you are plotting.
text(300,.25*exp(-0.005*300),... '\bullet\leftarrow\fontname{times}0.25{\ite}^{-0.005{\itt}} at {\itt} = 300',... 'FontSize',14)
This statement defines the text Position
property as
The default text alignment places this point to the left of the string and centered vertically with the rectangle defined by the text Extent
property. The following section provides more information about changing the default text alignment.
Adding Text Annotations to Graphs | Text Alignment |
© 1994-2005 The MathWorks, Inc.