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

2 vues (au cours des 30 derniers jours)
kyrollos yanny
kyrollos yanny le 20 Mai 2015
Commenté : kyrollos yanny le 22 Mai 2015
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 commentaire
Jan
Jan le 21 Mai 2015
"It does not seem to correctly get the values" is not a useful description. Please explain what happens. What is "input"?

Connectez-vous pour commenter.

Réponse acceptée

James Tursa
James Tursa le 21 Mai 2015
Modifié(e) : James Tursa le 22 Mai 2015
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 commentaires
James Tursa
James Tursa le 22 Mai 2015
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?"
kyrollos yanny
kyrollos yanny le 22 Mai 2015
I posted a new question under the same title you suggested. Thank you so much for your help.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Fortran with MATLAB dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by