Pass Strings

Passing strings from MATLAB® to a Fortran MEX-file is straightforward. The program revord.F accepts a string and returns the characters in reverse order. To see the example revord.F, open the fileopen the file in MATLAB Editor.

After checking for the correct number of inputs, the gateway routine mexFunction verifies that the input was a row vector string. It then finds the size of the string and places the string into a Fortran character array. For character strings, it is not necessary to copy the data into a Fortran character array using mxCopyPtrToCharacter. (mxCopyPtrToCharacter is a convenience function for working with MAT-files.)

To build this example, at the command prompt type:

mex revord.F


x = 'hello world';
y = revord(x)
y =

dlrow olleh
Was this topic helpful?