필터 지우기
필터 지우기

Mex/Fortran passing data

조회 수: 1 (최근 30일)
Chris
Chris 2012년 6월 26일
Hello,
Is it possible to pass from matlab data labeled as follows and if so what would be the efficient way to do it?
AeroConfig_Hub_position %a (3*1) vector
AeroConfig_Hub_orientation %a (3*3) matrix
AeroConfig_Hub_translationvel %a (3*1)vector
AeroConfig_Hub_rotationvel %a (3*1) vector
through a mex interface that will format them to be read by the fortan code as:
Aeroconfig%Hub%position
Aeroconfig%Hub%orientation
AeroConfig%Hub%rotationvel
AeroConfig%Hub%translationvel
any help is greatly appreciated.

채택된 답변

James Tursa
James Tursa 2012년 6월 26일
Download this package from the FEX:
Follow the instructions to compile the modules. Then you can do stuff like this:
m-code:
myFortranMexRoutine(AeroConfig_Hub_position,...
AeroConfig_Hub_orientation,...
AeroConfig_Hub_translationvel,...
AeroConfig_Hub_rotationvel);
Fortran code:
#include "fintrf.h"
subroutine mexFunction(nlhs,plhs,nrhs,prhs)
use MatlabAPImx
implicit none
integer nlhs, nrhs
mwPointer plhs(*), prhs(*)
__yourtype__ Aeroconfig
AeroConfig%Hub%position = fpGetPr1(prhs(1))
AeroConfig%Hub%orientation = fpGetPr(prhs(2))
AeroConfig%Hub%translationvel = fpGetPr1(prhs(3))
AeroConfig%Hub%rotationvel = fpGetPr1(prhs(4))
Caution: The above is just an outline with no error checking. In production code you would need to check nrhs and nlhs to see if there are the correct number of inputs & outputs. And you would have to retrieve the data pointers via fpGetPr1 and fpGetPr into separate variables and examine if they are associated prior to using them. Etc.
  댓글 수: 13
James Tursa
James Tursa 2012년 7월 4일
mxGetField returns a pointer to an mxArray, which you can then use just like any other pointer to an mxArray. E.g., if on the MATLAB side you did this:
myvar.myfield = 1:3
mymexroutine(myvar)
And if mymexroutine was the Fortran mex routine, then inside this mex routine you could have code like this to get at the data:
use MatlabAPImx
mwPointer mx
mwIndex i
real*8, pointer :: x(:)
i = 1
mx = mxGetField(prhs(1),i,"myfield")
x => fpGetPr1(mx)
The Fortran variable x will point to the 1:3 double data.
Chris
Chris 2012년 7월 4일
편집: Chris 2012년 7월 4일
Thanks James, so for a nested struct like turbinecomponents.hub.position, I would need to use the mxGetField twice, something like:
use MatlabAPImx
mwPointer mx, dx
mwIndex i
real*8, pointer :: x(:), (y:)
i= 1
mx = mxGetField(prhs(1),i,"Hub")
x = fpGetPr1(mx)
dx = mxGetField(prhs(1),i,"Position")
y = fpGetPr1(dx)

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Write C Functions Callable from MATLAB (MEX Files)에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by