How can my Fortran MEX program access a MATLAB complex matrix using the same memory location?

4 ビュー (過去 30 日間)

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
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 件)

カテゴリ

Help Center および File ExchangeFortran with MATLAB についてさらに検索

タグ

製品


リリース

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by