How can my Fortran MEX program access a MATLAB complex matrix using the same memory location?
4 ビュー (過去 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!