Department of Mathematics

Math 300: Mathematical Computing

Matlab Strings

The computing term "character string" refers to collections of characters that are handled by a computer program as an array. More particularly to Matlab, a character string is a vector of characters. Let's look at examples.

>> c = 'a'

assigns the single alphabetical character 'a' to a variable called c. By contrast,

>> c = 'alphabet 1'

assigns a collection of characters to a variable c. The variable c must necessarily be a vector in order to handle that collection, which means that we can refer to particular entries in that vector by index:

>> c(2) ans = l >> c(4) ans = h

Thus, the code c = 'alphabet 1' is completely equivalent to

>> c=['a' 'l' 'p' 'h' 'a' 'b' 'e' 't' ' ' '1'] c = alphabet 1

Each character is actually represented in the computer by an integer, with the alphabetical interpretation only happening when the string is printed to a screen or page. Thus, each string could equally well be viewed as a vector of integers. Indeed, we can add strings.

>> d='alphabet 2' d = alphabet 2 >> c+d ans = 194 216 224 208 194 196 202 232 64 99

Note that the addition took place elementwise. Thus, we see that '1' + '2' = 99. The way to understand this is that characters are associated with 'ASCII codes': the integers that represent them. Thus the number 1 as an integer is represented in the computer by an integer 1, but the character '1' is represented by the integer 49. The character '2' has ASCII code 50, so that '1' + '2' = 49 + 50 = 99. In the same way, you can see that the ASCII code for 'a' is 97, and the ASCII code for the space character is 32.

Often you will want include the results of computations in label or title strings. To do that, you must first convert the numbers to character strings, and then concatenate them to your label string. For example, consider making a label string that gives the second eigenvalue of a certain matrix.

>> A=[1 1; 2 1] A = 1 1 2 1 >> lam=eig(A) lam = 2.4142 -0.4142 >> mylabel = ['The second eigenvalue of A is ' num2str(lam(2))] mylabel = The second eigenvalue of A is -0.41421

There are several things happening here. First, observe that we use the function num2str to convert the floating point number in the variable lam(2) to a string. Second, we append the resulting string for that number to the text 'The second...' by putting them both in a vector together. Note that lam(2), being a number, only took up one storage location, i.e. one entry in the vector lam. By contrast, when we convert that number to a string, it takes up eight storage locations.

>> num = lam(2) num = -0.4142 >> length(num) ans = 1 >> str = num2str(lam(2)) str = -0.41421 >> length(str) ans = 8

Obviously the first character in str is '-', the second is '0', and so on. Note that the string we got from num2str contains one more digit of the number than was displayed in the floating point version. This is because Matlab does not always display all the digits of a floating point number, so there are some extra formatting defaults that occur in both numeric and string representations.

Consult the Matlab documentation for more details of this and related matters.

Assignment C is posted.

The test solution is available.

Department of Mathematics, PO Box 643113, Neill Hall 103, Washington State University, Pullman WA 99164-3113, 509-335-3926, Contact Us
Copyright © 1996-2015 Kevin Cooper