MEX - Multidimensional Array passing to Fortran
2 次查看(过去 30 天)
显示 更早的评论
Hello, I want to pass back and forth a MxN matrix with K number of pages from matlab to fortran. What mex function/commands would be used to do this? I can do the 2d arrays but haven't found anything for multidimensional arrays.
0 个评论
采纳的回答
James Tursa
2012-7-13
A couple of options:
--------------------------------------
1)
See this FEX submission:
To use a MATLAB 3D variable passed in to the mex function, e.g.:
use MatlabAPImx
real*8, pointer :: X(:,:,:)
X => fpGetPr3(prhs(1)) ! Does not copy the data
Then you can use X as a regular read-only Fortran variable since it is pointing directly at the data area of the original MATLAB variable in the workspace. I.e., no copy is made. If you need to alter the contents of X then use the copy version of the function instead with an appropriate deallocation at the end:
use MatlabAPImx
real*8, pointer :: X(:,:,:)
X => fpGetPr3copy(prhs(1)) ! Copies the data
! use/modify X
call fpDeallocate(X)
To copy a 3D Fortran array back to an output variable:
use MatlabAPImx
! X is a 3D Fortran real*8 variable
plhs(1) = mxArray(X)
To copy the input to a new matrix, modify it as a Fortran 3D variable, and return the result back to MATLAB you could do this:
use MatlabAPImx
real*8, pointer :: X(:,:,:)
plhs(1) = mxDuplicateArray(prhs(1)) ! Copies the data
X => fpGetPr3(plhs(1)) ! Points to the new copy
! use/modify X
--------------------------------------
2)
Manually get and copy the contents back & forth using mxGetNumberOfDimensions, mxGetDimensions, mxGetPr, mxCopyReal8ToPtr, mxCopyPtrToReal8, and Fortran allocated variables as necessary. Or some combination of the above with the %VAL() construct if you want to avoid data copies. It gets ugly so I will not attempt to write all of the details here.
2 个评论
James Tursa
2012-7-16
You cannot specify 'Blade(:,1)' as a field name. It has to be just the field name itself, e.g. 'Blade'.
mxGetDimensions is hard to work with in Fortran since it returns a pointer to an integer array and you can't work with it directly as is. You would need to pass it to a subroutine via %VAL() to be able to get at the dimension values.
Can you show what the declarations are for InputMarkers, Blade, and Orientation? I am not sure what you are trying to copy here.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Fortran with MATLAB 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!