mxGetString (C and Fortran)

String array to C-style string or Fortran character array

C Syntax

#include "matrix.h"
int mxGetString(const mxArray *pm, char *str, mwSize strlen);

Fortran Syntax

integer*4 mxGetString(pm, str, strlen)
mwPointer pm
character*(*) str
mwSize strlen



Pointer to a string mxArray; that is, a pointer to an mxArray having the mxCHAR_CLASS class.


Starting location for the string. mxGetString writes the character data into str and then, in C, terminates the string with a NULL character (in the manner of C strings). str can point to either dynamic or static memory.


Size in bytes of destination buffer pointed to by str. Typically, in C, you set strlen to 1 plus the number of elements in the string mxArray to which pm points. See the mxGetM and mxGetN reference pages to find out how to get the number of elements.

Do not use with Multibyte Encoded Characters.


0 on success or if strlen == 0, and 1 on failure. Possible reasons for failure include

  • mxArray is not a string array.

  • strlen is not large enough to store the entire mxArray. If so, the function returns 1 and truncates the string.


Call mxGetString to copy the character data of a string mxArray into a C-style string in C or a character array in Fortran. The copied string starts at str and contains no more than strlen-1 characters in C (no more than strlen characters in Fortran). In C, the C-style string is always terminated with a NULL character.

If the string array contains several rows, the function copies them into one long string array, one column at a time.

Multibyte Encoded Characters

Use this function only with strings represented in single-byte encoding schemes. For strings represented in multibyte encoding schemes, use the C function mxArrayToString. Fortran users must allocate sufficient space for the return string to avoid possible truncation.


See the following examples in matlabroot/extern/examples/mx.

See the following examples in matlabroot/extern/examples/mex.

See the following examples in matlabroot/extern/examples/refbook.

Was this topic helpful?