Passing a String from Matlab to a Fortran Mex Function

1 visualización (últimos 30 días)
Rick
Rick el 4 de Dic. de 2014
Comentada: Geoff Hayes el 9 de Dic. de 2014
I am passing a string from a Matlab routine to a Fortran mex function. However, I am not having much luck pulling it out in the Fortran.
% My Simple Matlab Code:
%
temp_string = 'This is a place of business';
Call_String_Test(temp_string);
Could someone please help me to get this string pulled out and printed in the Fortran mex function. I have tried mxgetstring, mxcopyptrtocharacter, mxgetchars, and others. I can't seem to get the write syntax. I either get compiler errors, or matlab blows up. TIA, RICK

Respuesta aceptada

Geoff Hayes
Geoff Hayes el 7 de Dic. de 2014
Hi Rick - the revord.F example does a decent job of describing the steps to read the input string. It can be opened in your editor as
edit([matlabroot '/extern/examples/refbook/revord.F']);
The code that follows is a variation of the this function. Note that I had problems with the call mexErrMsgIdAndTxt ... calls crashing MATLAB, so I just replaced them with a mexPrintf equivalent.
#include "fintrf.h"
C Gateway routine
subroutine mexFunction(nlhs, plhs, nrhs, prhs)
C mexFunction arguments:
mwPointer plhs(*), prhs(*)
integer nlhs, nrhs
C Function declarations:
mwPointer mxCreateString
mwPointer mxGetString
mwPointer mxGetM, mxGetN
integer mxIsChar
C Array information:
mwPointer mrows, ncols
C Arguments for computational routine:
mwSize maxbuf
parameter(maxbuf = 100)
character*120 line
integer*4 k
character*100 input_buf
mwPointer strlen
C Check for proper number of arguments.
if(nrhs .ne. 1) then
write(line,*) 'error: incorrect number of input arguments'
k=mexPrintf(line//achar(13))
return
C The input must be a string.
elseif(mxIsChar(prhs(1)) .ne. 1) then
write(line,*) 'error: input argument is not a string'
k=mexPrintf(line//achar(13))
return
endif
C Get the size of the input string.
mrows = mxGetM(prhs(1))
ncols = mxGetN(prhs(1))
C Get the length of the input string and validate.
strlen = mrows*ncols
if (strlen .gt. maxbuf) then
write(line,*) 'error: input is greater than max str size'
k=mexPrintf(line//achar(13))
return
endif
C Get the string.
status = mxGetString(prhs(1), input_buf, maxbuf)
write(line,*) 'str = ', input_buf
k=mexPrintf(line//achar(13))
return
end
The code checks to make sure that there is only one string input. Note how the input string can only have a fixed number of characters. I saved the above to a file called readString.F and compiled as mex readString.F. In the Command Window, I called this function as
>> readString(1)
error: input argument is not a string
>> readString('hello',2)
error: incorrect number of input arguments
>> readString('hello')
str = hello
>> readString('This is a place of business')
str = This is a place of business
See the attached for a working copy of this code.
  2 comentarios
Rick
Rick el 8 de Dic. de 2014
Thank YOU Geoff !!!!!!!!!!! I am now GOOD to GO, thanx......... :-)
Geoff Hayes
Geoff Hayes el 9 de Dic. de 2014
Glad it worked, Rick!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Write C Functions Callable from MATLAB (MEX Files) en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by