Using the C Math Library | ![]() ![]() |
Accessing Individual Strings in an Array of Strings
You can manipulate multidimensional character arrays just as you would a standard MATLAB numeric array. For example, to extract an individual string from a character array, use standard MATLAB indexing syntax. Note, however, that a string extracted from a character array in this fashion may contain blank padding characters. To remove these blank characters from the character array, use the mlfDeblank()
routine.
The following code fragment extracts the string "my dog
" from the character array, A
, created in the previous section. This is equivalent to the MATLAB statement B = A(2,:)
. The example displays the size of the extracted array, B
, before and after removing blanks. Note that the index format string is passed as a standard C string; it does not need to be a MATLAB character array.
mxArray *B = NULL; mlfAssign(&B,mlfIndexRef(A, "(?,?)", /* index format string */ mlfScalar(2), /* row two */ mlfCreateColonIndex())); mlfPrintMatrix(mlfSize(NULL,B,NULL)); mlfPrintMatrix(mlfSize(NULL,mlfDeblank(B),NULL)); mlfPrintMatrix(B); mxDestroyArray(B);
This code produces the following output.
1 9 1 6
![]() | Creating MATLAB Character Arrays | Cell Arrays | ![]() |