How can my Fortran MEX program access a MATLAB complex matrix using the same memory location?
4 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
MathWorks Support Team
am 24 Apr. 2025
Beantwortet: MathWorks Support Team
am 24 Apr. 2025
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?
Akzeptierte Antwort
MathWorks Support Team
am 24 Apr. 2025
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 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Fortran with MATLAB finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!