How can my Fortran MEX program access a MATLAB complex matrix using the same memory location?
8 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
MathWorks Support Team
el 24 de Abr. de 2025
Respondida: MathWorks Support Team
el 24 de Abr. de 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?
Respuesta aceptada
MathWorks Support Team
el 24 de Abr. de 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 comentarios
Más respuestas (0)
Ver también
Categorías
Más información sobre Fortran with MATLAB en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!