How can my Fortran MEX program access a MATLAB complex matrix using the same memory location?
5 次查看(过去 30 天)
显示 更早的评论
MathWorks Support Team
2025-4-24
回答: MathWorks Support Team
2025-4-24
I am working on Fortran with MATLAB, and I want my Fortran program to use the result of a MATLAB operation, which is a complex matrix.
I cannot copy the matrix due to memory constraints, so I would like the Fortran program to be able to access the memory location of the MATLAB variable.
How can I achieve this?
采纳的回答
MathWorks Support Team
2025-4-24
You can achieve this using the Fortran %val construct.
See the attached "complexAddValInter.F" for an example. This example takes two MATLAB complex arrays of equal size as inputs and returns a complex array representing their sum.
This example only works with interleaved complex arrays, so you need to include the "-R2018a" flag when compiling.
mex -R2018a complexAddValInter.F
You can safely ignore the following warning:
C:\Users\gkepler\OneDrive - MathWorks\Documents\Scripts\Mex\Fortran\complexAddValInter.F:115:72:
call addMatrixVal(%VAL(prhs1),%VAL(prhs2),plhs1,nl)
1
Warning: Type mismatch in argument 'complexdatafirstmatrix' at (1); passed INTEGER(8) to COMPLEX(8) [-Wargument-mismatch]
You can test the code in the following way.
a1=randi(5,2); b1=randi(3,2); c1=complex(a1,b1);
a2=randi(2,2); b2=randi(5,2); c2=complex(a2,b2);
c = complexAddValInter(c1,c2)
0 个评论
更多回答(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!