Simple 2-D Plot Manipulation (Plotting in Two Dimensions) (MATLAB) Part 3

Special Text Character Formats

You’ve seen that adding a string of text is relatively easy. You’ve even seen that you can have text in a title or on an axis label with more than one line by storing your strings in cell arrays. But in all these cases the text we provided were those characters available directly from our keyboard. What about special characters like the Greek alphabet, superscripts, subscripts, arrows, and other mathematical sysmbols?

Fortunately MATLAB has the capability of modifying text to have different styles. It does this by providing support for a subset of the TeX characters. TeX is a standard notation for special character sets. You can recognize it right away as it will have a backslash "\" prefixing a character name. For example, the Greek character Ω (big omega) is specified by "\Omega" in TeX. Table 3.4.2 lists all the TeX characters available in MATLAB.

Table 3.4.2 TeX Characters Available in MATLAB

TeX

Characters

Result

TeX

Characters

Result

TeX

Characters

Result

\alpha

tmp5c05-99

\upsilon


tmp5c05-100

\sim

tmp5c05-101

\beta

tmp5c05-102

\phi

tmp5c05-103

\leq

tmp5c05-104

\gamma

tmp5c05-105

\chi

tmp5c05-106

\infty

tmp5c05-107

\delta

tmp5c05-108

\psi

tmp5c05-109

\clubsuit

tmp5c05-110

\epsilon

tmp5c05-111

\omega

tmp5c05-112

\diamondsuit

tmp5c05-113

\zeta

tmp5c05-114

\Gamma

tmp5c05-115

\heartsuit

tmp5c05-116

\eta

tmp5c05-117

\Delta

tmp5c05-118

\spadesuit

tmp5c05-119

\theta

tmp5c05-120

\Theta

tmp5c05-121

\leftrightarrow

tmp5c05-122

\vartheta

tmp5c05-123

\Lambda

tmp5c05-124

\leftarrow

tmp5c05-125

\iota

tmp5c05-126

\Xi

tmp5c05-127

\uparrow

tmp5c05-128

\kappa

tmp5c05-129

\Pi

tmp5c05-130

\rightarrow

tmp5c05-131

\lambda

tmp5c05-132

\Sigma

tmp5c05-133

\downarrow

tmp5c05-134

\mu

tmp5c05-135

\Upsilon

tmp5c05-136

\circ

tmp5c05-137

\nu

tmp5c05-138

\Phi

tmp5c05-139

\pm

tmp5c05-140

\xi

tmp5c05-141

\Psi

tmp5c05-142

\geq

tmp5c05-143

\pi

tmp5c05-144

\Omega

tmp5c05-145

\propto

tmp5c05-146

\rho

tmp5c05-147

\forall

tmp5c05-148

\partial

tmp5c05-149

\sigma

tmp5c05-150

\exists

tmp5c05-151

\bullet

tmp5c05-152

\varsigma

tmp5c05-153

\ni

tmp5c05-154

\div

tmp5c05-155

\tau

tmp5c05-156

\cong

tmp5c05-157

\neq

tmp5c05-158

\equiv

tmp5c05-159

\approx

tmp5c05-160

\aleph

tmp5c05-161

\Im

tmp5c05-162

\Re

tmp5c05-163

\wp

tmp5c05-164

\otimes

tmp5c05-165

\oplus

tmp5c05-166

\oslash

tmp5c05-167

\cap

tmp5c05-168

\cup

tmp5c05-169

\supseteq

tmp5c05-170

TeX

Characters

Result

TeX

Characters

Result

TeX

Characters

Result

\supset

tmp5c05-171

\subseteq

tmp5c05-172

\subset

tmp5c05-173

\int

tmp5c05-174

\in

tmp5c05-175

\o

tmp5c05-176

\rfloor

tmp5c05-177

\lceil

tmp5c05-178

\nabla

tmp5c05-179

\lfloor

tmp5c05-180

\cdot

tmp5c05-181

\dots

tmp5c05-182

\perp

tmp5c05-183

\neg

tmp5c05-184

\prime

tmp5c05-185

\wedge

tmp5c05-186

\times

tmp5c05-187

\0

tmp5c05-188

\rceil

tmp5c05-189

\surd

tmp5c05-190

\mid

tmp5c05-191

\vee

tmp5c05-192

\varpi

tmp5c05-193

\copyright

tmp5c05-194

\langle

tmp5c05-195

\rangle

tmp5c05-196 tmp5c05-197

In addition to recognizing the special characters already listed, the MATLAB TeX interpreter also recognizes the following stream modifiers that control the font used.

Table 3.4.3 TeX Stream Modifiers

TeX Stream Modifier

Description

tmp5c05-198

Bold font.

tmp5c05-199

Italics font.

tmp5c05-200

Oblique font (rarely used).

tmp5c05-201

Normal font.

tmp5c05-202

Make part of string superscript.

tmp5c05-203

Make part of string subscript.

\fontname{fonfname}

Specify the font family to use.

\fontsize{fonfs/ze}

Specify the font size in FontUnits.

The first four modifiers are mutually exclusive so you can’t use them together. However, you can use \fontname in combination with one of the other modifiers. Also, stream modifiers remain in effect until the end of the string or only within the context defined by braces { }. The following code illustrates using the TeX interpreter and produces the plot shown in Figure 3.31.

tmp8414204_thumb[2][2]

 

 

 

Using TeX for special characters.

Figure 3.31 Using TeX for special characters.

What about if you want to printtmp8414206_thumb[2][2]Since    these    have meaning in the TeX interpreter you will need to tell the interpreter to ignore the command. This is achieved by using a backslash "\" right before them.

Using Subplot to Create Multiple Axes

You’ve seen that you can have multiple plots on an axis, either by plotting multiples or by using the hold command and issuing another plot command. However, you are not limited to having one axes object in a Figure Window. The easiest way to create multiple axes in a Figure Window is to make use of the command subplot. This function breaks up the Figure Window’s space into subregions or panes and is very useful for showing related information that is better viewed in individual plots. Calling the command with three arguments creates these subregions; the first two specify how many regions there will be in terms of rows and columns, and the third argument specifies which region you wish to plot in. For example, subplot(m,n,p) subdivides the Figure Window into m-by-n regions and creates axes in the pth region, where regions are numbered from left to right and top to bottom within the figure. For example, the following will break up the Figure Window into three distinct regions and create an axes object in the second one.

tmp8414207_thumb[2][2]

After you have created an axes object in one of the regions, you can then use any plotting command you want. The axes created with subplot can be treated in the same way as the ones that are created when no subregions are specified. In fact, you can create an axes object which encompasses the entire Figure Window’s space by issuing subplot(1,1,1) and get the same axes that you would have gotten by using plot commands without the subplot function.

If an axes already exists in the subregion identified with the subplot command, the existing axes becomes the current axes to which all subsequent graphics commands are issued. You can flip back and forth between these regions by re-issuing the subplot command to set one of the other axes or regions as the current axes.

As an example, let’s break up the figure space into four panes configured in a 2-by-2 fashion. The following code will create a figure space with four subregions configured as a 2-by-2. We will plot three different shapes separately in the first three regions and then have the fourth subregion contain all three shapes superimposed on top of one another.

tmp5c05-210

Figure 3.32 shows the results of this script.

Multiple axes with subplot.

Figure 3.32 Multiple axes with subplot.

Keep in mind, that if at any time you create a subplot that breaks up the figure into a new M-by-N configuration, the set of existing axes will be deleted!

A useful (and undocumented) manipulation of the subplot can be used to place a different number of subplots on a row. For instance, instead of having four subplots in a two-by-two matrix, we could have two subplots on the top row and one subplot on the bottom row that spans the figure. The following code will create the plot shown in Figure 3.33.

tmp8414212_thumb[2][2]

The first two uses of subplot appear familiar enough; however, the final call specifies the large axis shown at the bottom of the figure. To create a similar figure with the single plot at the top use:

tmp8414213_thumb[2][2]

 

 

Odd axis made with subplot.

Figure 3.33 Odd axis made with subplot.

To create a subplot with one large plot axis on the left side and two small plot axes in a column on the right, use:

tmp8414215_thumb[2][2]

After you have created some subplots, you may have noticed that if you use the title command a title is created on top of the current axes. In some cases you may wish to have a title that is centered at the top of the Figure Window instead. Certainly you could use any of the text placement functions that were discussed previously such as gtext and text. However, if you do not want to be prompted or waited upon to place the text as with gtext, or you don’t want to determine the desired position’s relative location to the current axes as which would need to be done with text, you can use a function we include here called toptitle. This function will perform the calculations required to place a title string at the top of the figure regardless of the region to which you are currently plotting. The format for using toptitle is simply toptitle(string_vector) where string_vector is a character string containing the set of characters that you wish to have appear at the top of the figure.

tmp5c05-216

Next post:

Previous post: