Characters and Text

Enter text into MATLAB using single quotes. For example,

The result is not the same kind of numeric matrix or array we have been dealing with up to now. It is a 1-by-5 character array.

Internally, the characters are stored as numbers, but not in floating-point format. The statement

converts the character array to a numeric matrix containing floating-point representations of the ASCII codes for each character. The result is

The statement

reverses the conversion.

Converting numbers to characters makes it possible to investigate the various fonts available on your computer. The printable characters in the basic ASCII character set are represented by the integers 32:127. (The integers less than 32 represent nonprintable control characters.) These integers are arranged in an appropriate 6-by-16 array with

The printable characters in the extended ASCII character set are represented by F+128. When these integers are interpreted as characters, the result depends on the font currently being used. Type the statements

and then vary the font being used for the MATLAB command window. On a PC or Mac, select Preferences under the File menu. Be sure to try the Symbol and Wingdings fonts, if you have them on your computer. Here is one example of the kind of output you might obtain.

Concatenation with square brackets joins text variables together into larger strings. The statement

joins the strings horizontally and produces

The statement

joins the strings vertically and produces

Note that a blank has to be inserted before the 'w' in h and that both words in v have to have the same length. The resulting arrays are both character arrays; h is 1-by-11 and v is 2-by-5.

To manipulate a body of text containing lines of different lengths, you have two choices - a padded character array or a cell array of strings. The char function accepts any number of lines, adds blanks to each line to make them all the same length, and forms a character array with each line in a separate row. For example

produces a 5-by-9 character array

There are enough blanks in each of the first four rows of S to make all the rows the same length. Alternatively, you can store the text in a cell array. For example

is a 5-by-1 cell array

You can convert a padded character array to a cell array of strings with

and reverse the process with

Structures

Structures are multidimensional MATLAB arrays with elements accessed by textual field designators. For example,

creates a scalar structure with three fields.

Like everything else in MATLAB, structures are arrays, so you can insert additional elements. In this case, each element of the array is a structure with several fields. The fields can be added one at a time,

Or, an entire element can be added with a single statement.

Now the structure is large enough that only a summary is printed.

There are several ways to reassemble the various fields into other MATLAB arrays. They are all based on the notation of a comma separated list. If you type

it is the same as typing

This is a comma separated list. Without any other punctuation, it is not very useful. It assigns the three scores, one at a time, to the default variable ans and dutifully prints out the result of each assignment. But when you enclose the expression in square brackets,

it is the same as

which produces a numeric row vector containing all of the scores.

Similarly, typing

just assigns the names, one at time, to ans. But enclosing the expression in curly braces,

creates a 1-by-3 cell array containing the three names.

And

calls the char function with three arguments to create a character array from the name fields,