Thursday, November 20, 2008

Strings: I


Handling Strings

Ability to handle strings assumes most importance when 'cleaning' data and saving results.

Most large sized data sets suffer from repetitions, missing values, wrong entries and all kinds of fancy errors. Although one can check for some kind of errors (unexpectedly large or small values) using comparisons with numbers (eps, Inf etc), most others defy any sensible pattern. In most cases, then, string comparison is the only recourse available.

The more obvious uses of string handling are the flexibility it allows in choosing dynamic names for files/variables in which to store results and manipulating data labels.

Let's start with basics.

Character Arrays

The assignment:

>> char1 = 'A';

assigns the value A to the 1 X 1 character array char1 (of size 2 bytes). Similarly,

>> char2 = 'BCDE'

assigns the value BCDE to the 1 X 4 character array char2 (of size 8 bytes).

Now one can play around with char1 and char2 like any other numeric matrix or vector. SoL

>> char3 = [char1 char];

gives

char3 =

ABCDE

Of course, one can access individual elements of arrays too. For example:

>> char4 = [char1 char2(2 : end)];

gives

char4 =

ACDE

The same can also be achieved by the function:

strcat

>> strcat(char1, char2(2:end));

or

>> strcat('A', 'CDE');

Another similar function is:

strvcat

As you would have probably guessed, this concatenates characters one below the other vertically. So:

>> strvcat(char1, char2(2 : end));

gives

ans =

A
CDE

If, however, one is not going to use strvcat, then concatenating requires that each row of the array must be of the same length. Following is an example:

>> x = ['AAA'; 'BBB'; 'CCC']

Another way to write the above command is:

>> x = ['AAA'; ...
'BBB'; ...
'CCC'];

To make an array of strings that are originally of unequal length in the above manner, you must either pad the shorter strings with extra space characters, or use strvcat. Another way is to use:

char

char automatically pads the shorter strings with extra space. For example, while the following gives an error:

>> x = ['AAA'; 'BBBBBB'; 'CCC'];

the following is ok:

>> x = char(['AAA'; 'BBBBBB'; 'CCC']);

Some other useful related functions include:

ischar: determines whether the input is a character array

isletter: finds all the alphabetic letters in the input string

isspace: finds all the blanks in a sentence. For example:

>> x = 'test for blanks';

>> find(isspace(x))

gives:

ans =

5 9

(fifth and ninth 'letter' of the sentence is space).

blanks: allows you to create a string of blank characters

deblank: allows you to remove all the trailing space charcters (at the end of a phrase or a sentence)

Cell Array of Strings

There is another type of MATLAB array, the cell array, can hold different sizes and types of data in an array without padding. Cell arrays provide a more flexible way to store strings of varying length.

cellstr

converts a character array of strings into a cell array of string

iscellstr

checks if the input is a cell array of string, and

char

can be used on a cell array of strings to go back to a character array

There are yet more useful functions to manipulate and play around with strings, in the interest of time, for now I think we can end by these final two commands/functions

strcmp

is a logical command compares two strings if they are exactly equal. If one is comparing arrays, then both arrays should have the same dimension. In that case
strcmp compares element of the arrays separately and returns 1 for places where it finds a match. The syntax is:

strcmp('string1', 'string2');

strmatch

has the syntax:

strmatch('string', 'array')

As the name suggests, it looks through the rows of the character array or cell array of strings to find strings that begin with the text contained in string, and returns the matching row indices. For example (taken from Mathworks site), the statement

>> x = strmatch('max', strvcat('max', 'minimax', 'maximum'))

returns

x = [1; 3] since rows 1 and 3 begin with 'max'.

The statement

>> x = strmatch('max', strvcat('max', 'minimax', 'maximum'), 'exact')

returns x = 1, since only row 1 matches 'max' exactly.

No comments: