Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

Call Matlab from Fortran

John_H_18
Beginner
541 Views

This is related to the answer to this question https://software.intel.com/en-us/forums/intel-fortran-compiler-for-linux-and-mac-os-x/topic/590181.

One of the answers was to use

call execute_command_line(" matlab -nodesktop -nojvm -r 'Matlab_script; exit' ")

However, there are two things which I'm not sure are possible with this

  1. Can I call the same instance of Matlab each time?
  2. Can I export data from Fortran into Matlab, then return the result of the function to Fortran?

Or does someone have a working example of a Fortran code that calls Matlab? Because I cannot get Matlab's fengdemo.F to not segFault using ifort.

0 Kudos
1 Solution
mecej4
Honored Contributor III
541 Views

You did not state the versions of the OS, Intel compilers and Matlab, and the number of possible combinations is large.

On OpenSuse 13.2 X64, with Matlab 2007b X64 and the current Intel compiler, I could build and run the fengdemo.F example. To do this, I had to open an IFort shell window, add the Matlab bin directory to PATH and  Matlab bin/<arch> to LD_LIBRARY_PATH. I built the a.out with

   ifort -i8 -o fengdemo -I <ML_dir>/extern/include fengdemo.F -L <ML_dir>/bin/glnxa64/ -lmx -leng

You probably got seg-faults because you left out the -i8 option with a 64-bit version of Matlab. These 8-byte integer variables in the Fortran code are actually pointers, which have to be 64-bits long as the comment in fengdemo.F says.

View solution in original post

0 Kudos
4 Replies
mecej4
Honored Contributor III
542 Views

You did not state the versions of the OS, Intel compilers and Matlab, and the number of possible combinations is large.

On OpenSuse 13.2 X64, with Matlab 2007b X64 and the current Intel compiler, I could build and run the fengdemo.F example. To do this, I had to open an IFort shell window, add the Matlab bin directory to PATH and  Matlab bin/<arch> to LD_LIBRARY_PATH. I built the a.out with

   ifort -i8 -o fengdemo -I <ML_dir>/extern/include fengdemo.F -L <ML_dir>/bin/glnxa64/ -lmx -leng

You probably got seg-faults because you left out the -i8 option with a 64-bit version of Matlab. These 8-byte integer variables in the Fortran code are actually pointers, which have to be 64-bits long as the comment in fengdemo.F says.

0 Kudos
John_H_18
Beginner
541 Views

It was a combination of two things. The first was I wasn't using -i8, the second was I was using ifort 14, to respond to your question I tried ifort 15 and that ended up working.

0 Kudos
JVanB
Valued Contributor II
541 Views

That fengdemo.F example looks really old-fashioned. Can't it be upgraded with C interoperability so as to avoid the -i8 switch and preprocssing altogether?

 

0 Kudos
IanH
Honored Contributor II
541 Views

Yes (replace "old fashioned" with "horrendous").  However the interfaces for the Matlab side of things are provided by Mathworks, so if you want to go down that path (which is what I did for the aspects of the Matlab interface relevant to me - which I don't think covered the engine API) then you need to the BIND(C) conversion work yourself and perhaps remove yourself from what the Mathworks would support. 

(It has been a while since I looked at these, they may not be complete, they may be buggy, particularly the Matlabutils module, where I can see lots of copy-pasta work that hasn't been fin

 

0 Kudos
Reply