This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

mxGetString (C and Fortran)

mxChar 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

#include "fintrf.h"
integer*4 mxGetString(pm, str, strlen)
mwPointer pm
character*(*) str
mwSize strlen



Pointer to an mxChar array.


Starting location. 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 mxArray to which pm points. To get the number of elements, use mxGetM or mxGetN.

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 an mxChar 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 an mxArray into a C-style string in C or a character array in Fortran. The copied data 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 array contains multiple rows, the function copies them into a single array, one column at a time.

Multibyte Encoded Characters

Use this function only with characters represented in single-byte encoding schemes. For characters 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.

Introduced before R2006a

Was this topic helpful?