Using the C Math Library    

Creating MATLAB Character Arrays

Wherever you pass a character string to a MATLAB C Math Library routine, the string must be a MATLAB character string array, not a standard C null-terminated character string. MATLAB represents characters in 16-bit, Unicode format.

Using Explicit Character Array Creation Routines

The easiest way to create a MATLAB character string is with the MATLAB API routine mxCreateString(). You pass this routine a standard C character string as an argument, delimited by double quotation marks. (In the MATLAB interpreted environment, strings are delimited by single quotation marks.)

This code produces the following output.

Converting Numeric Arrays to Character Arrays

To convert a numeric array into a character array, use the mlfChar() routine. The following code creates an array containing the ASCII codes for each character in "my string" and then call mlfChar() to convert this numeric array into a MATLAB character array.

This code produces the following output.

To convert this character array back into its underlying numeric representation in double precision format, use the mlfDouble() routine.

Creating Multidimensional Arrays of Strings

You can create a multidimensional array of MATLAB character strings; however, each string must have the same length. The MATLAB C Math Library routines that create arrays of character strings pad the strings with blanks to make them all a uniform length.

To illustrate, the following code fragment creates a two-dimensional array character from two strings of different lengths.

As the following output illustrates, mlfChar() creates an 2-by-9 character array. This indicates that it added three blanks characters to the string "my dog" to make it the same length as "my string" .

You can also use the mlfStrcat(), mlfStrvcat() and mlfStr2mat() routines to group strings into a multidimensional character array. For more information about these routines, see the online MATLAB C Math Library Reference.


 Character Arrays Accessing Individual Strings in an Array of Strings