Getting the values of a Matlab Structure using a FORTRAN MEX file

Hi, I am trying to pass a matlab structure to a FORTRAN subroutine. In my gateway routine, I am trying to get the values of the matlab structure and store it in a fortran array. using this code
mwpointer x_ptr(72)
mwIndex index
index =1
do 40 i=1,72
x_ptr(i)=mxGetFieldByNumber(prhs(1),index,i)
call mxCopyPtrToReal4(x_ptr, input(i), nelem)
40 continue
Ps: the structure has 72 fields. however, it does not seem to correctly get the values from the matlab structure and place it in the fortran array. Can someone please help?

1 Comment

"It does not seem to correctly get the values" is not a useful description. Please explain what happens. What is "input"?

Sign in to comment.

 Accepted Answer

A few comments:
(1) I would not use "index" for a variable name, since that is the name of a Fortran instrinsic function. Not an error, per se, but it does make the code less readable.
(2) You don't show all of the variable declarations, or how you build the structure at the m-file level, so I will have to assume that everything is OK there. I.e., the passed in structure really does have 72 fields, and that each field contains a single array of "nelem" elements. You don't set nelem in your post, so I assume this is done but not posted?
(3) The x_ptr(i) is the address of the mxArray variable in the field ... it is NOT the address of the single data in that variable. You need to wrap a mxGetData call around that to get the address of the single data. E.g.,
mwPointer, external :: mxGetData
real input(72)
mwSize :: nelem = 1
:
call mxCopyPtrToReal4(mxGetData(x_ptr(i)), input(i), nelem)
(4) The use of "input(i)" as the target for the data copy in the above line only makes sense to me if nelem = 1 and you are copying a bunch of individual single scalars into the Fortran REAL array input. Is this true?
(5) To make your code robust against invalid input, you should check all of your inputs before using them. E.g., check nrhs and nlhs to see if there are the expected number of inputs and outputs. Check prhs(1) to see if it is a struct. Check to see that there are 72 fields. Check each field as you get it to see that it is not NULL (0 in Fortran) and actually contains a scalar single value. Etc, etc.

3 Comments

Thank you so much for your answer. I tried your suggestion and it worked. Also you are right, nelem=1, im just reading scalar values. I have a follow up question if you do not mind. If im trying to get a character value from a field (ex input.value='title') and I use this code:
character*32 title
mwpointer titleptr
count=1
n=32
nelem=1
titlePtr=mxGetFieldByNumber(prhs(1), 1, 1)
call mxCopyPtrToCharacter(mxGetData(titleptr),title,n)
It only assigns the first letter 't' to the variable title. Do you know how to copy all the characters in the field? Thank you so much in advance.
I can explain all of this, but the answer is not trivial and is somewhat unrelated to the original Question of this post. Please open up a new Question for this. That will get the content under an appropriate title for searching. E.g., maybe a title like "How do you copy MATLAB strings to Fortran character variables?"
I posted a new question under the same title you suggested. Thank you so much for your help.

Sign in to comment.

More Answers (0)

Categories

Tags

Community Treasure Hunt

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

Start Hunting!