Calling MATLAB from Intel Visual Fortran Code

3 views (last 30 days)
V1
V1 on 1 May 2013
Hi all,
I went through pages and pages of MATLAB and Fortran linking instructions,on MEX, MATLAB engine etc, I cant get a good grasp on a clear instruction to do so?
I use Intel Visual Fortran to program my code, modified on Microsoft Visual Studio, and to execute my program I open 'Fortran Build Environment for applications running on Intel® 64' through command: ifort fortrancodename.for
Now my question I would like to use some inbuilt functions on MATLAB (EXAMPLE [A,B]=eig(b)) - and reinterpret the solution back into fortran. Wishfully something like:
...
(Fortran Code with variable A B C D)
...
CALL-INSERT-COMMAND-INTO-MATLAB-ENGINE (eig([A B;C D])
...
(Fortran Code with variable A B C D and ANS)
...
Impossible? Possible?
Thanks

Answers (1)

James Tursa
James Tursa on 1 May 2013
What you are looking to do is possible. The two general ways of going about things are:
-----------------------
mex "function" approach:
-----------------------
The Fortran code is basically turned into one big subroutine that is called from MATLAB. This subroutine, also known as a mex function, is a dll that gets connected to your MATLAB session at runtime. It has a specific interface (mexFunction) that you can find in the doc under External Interfaces. This mex function is part of your MATLAB process, so it uses the same address space (has direct access to MATLAB variable memory, etc). You can call MATLAB functions from within this Fortran code through the mexCallMATLAB function.
-----------------------
mex "Engine" approach:
---------------------
The Fortran code is a program that is a completely separate process from your MATLAB session. It does not share the same address space. It communicates with your separate MATLAB process via a COM interface. To call MATLAB functions from your program you need to copy over all the variables under user-defined variable names, then evaluate a string on the MATLAB side using those variable names (engEvalString), then copy the results back to your Fortran program. You can sometimes end up with triple copies of your variables under this scheme, and of course there is the extra runtime involved with the copying itself. There is a submission on the FEX that does this work for you. It can be found here:
------------------------------------------------------------
The compiling of mex function vs mex Engine is a bit different also. They both use the mex command but with different options files.
In either approach, you will need to convert your Fortran variables to MATLAB mxArray variables, since that is what the MATLAB functions need for input. And then you will need to convert the mxArray results back to Fortran arrays. The standard way of doing these conversions is through the use of the mxCopyPtrToReal8 function (and its cousins). This is a bit messy. An alternative approach is to use the MATLAB Fortran 95 interface that I have written. It allows you to form Fortran pointers that point directly at MATLAB mxArray variable data areas, often avoiding the need to make a copy at all. You can find this package on the FEX here:

Categories

Find more on Fortran with MATLAB in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!