Convert arrays to strings
C Syntax
#include "matrix.h"
char *mxArrayToString(const mxArray *array_ptr);
Arguments
array_ptr
Pointer to a string mxArray
; that is, a pointer to an mxArray
having the mxCHAR_CLASS
class.
Returns
A C-style string. Returns NULL on out of memory.
Description
Call mxArrayToString
to copy the character data of a string mxArray
into a C-style string. The copied C-style string starts at buf
and contains no more than buflen-1
characters. The C-style string is always terminated with a NULL
character.
If the string array contains several rows, they are copied, one column at a time, into one long string array. This function is similar to mxGetString
, except that:
mxArrayToString
does not free the dynamic memory that the char
pointer points to. Consequently, you should typically free the string (using mxFree
) immediately after you have finished using it.
Example
Use mxArrayToString
to convert the data from a string array into a C string named buf
:
void
mexFunction(int nlhs,mxArray *plhs[],int nrhs,const mxArray
*prhs[])
{
char *buf;
/* Copy the string data from prhs[0] and place it into buf. */
buf= mxGetString(prhs[0]);
if (buf != NULL)
mexPrintf("The converted string is \n%s.\n", buf);
else
mexErrMsgTxt("Could not convert string data.");
/* Manipulate buf as you would manipulate any C string. */
...
/* When finished, free buf memory. */
mxFree(buf);
}
For an additional example, see mxarraytostring.c
in the mx
subdirectory of the examples
directory.
See Also
mxCreateCharArray
, mxCreateCharMatrixFromStrings
, mxCreateString
, mxGetString
[ Previous | Help Desk | Next ]