Community
cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
Beginner
37 Views

Fortran-Matlab interface

Hello,

 

 

I am very new to Fortran. I am building some Fortran codes to run some post-processing of data acquired by an ultrasound system that is controlled by Matlab. I cannot get away with Matlab because it is where the data is coming from. So far my solution is to use Matlab's "fopen" and "fwrite" functions to write the data into a file on the hard drive, and then read in the file using my Fortran codes. I have two questions:

1. I am running Matlab 2014a on a Mac OS (10.10) with Intel Fortran 2016 (compilers and libraries 2016.3.170). Under terminal window, I could run the Unix executable fine. But in Matlab's command window, when I run "./myfunction", it gives me the error below:

           dyld: Library not loaded: @rpath/libmkl_intel_lp64.dylib
           Referenced from: /Volumes/...
           Reason: image not found

I know that under the terminal window, I have to always call "source bin/compilervars.sh intel64". I did this in Matlab's command window. It    would run but would still gives me the error above when I tried executing my function. Any ideas on how to do this?

2. The Matlab "fopen" and "fwrite" solution is not optimal. I was thinking about building my Fortran codes with Matlab's mex function, but mex doesn't seem to support Fortran 2016 on a Mac OS X. Any suggestions on how to efficiently run a Fortran executable that is interacting with Matlab? The main interaction between the two is data input and data output: Matlab raw data -> my Fortran function for processing -> output to Matlab. 

Thank you!

 

0 Kudos
5 Replies
Highlighted
Black Belt
37 Views

I have no experience with running Matlab on OSX, so my remarks may not apply.

Are you running a 32-bit version of Matlab, or a 64-bit version? How do you launch Matlab?

On Linux, I would suggest opening up a shell window, sourcing the Intel startup script, adding the Matlab BIN directory to PATH, adding the directory containing the Matlab shared libraries to LD_LIBRARY_PATH and then running the Matlab application from the same command window.

Please expand on why you think it is bad to have your Fortran application run as a post-processor. Are the data files being exchanged of very large size? How many times do you run the post-processor?

0 Kudos
Highlighted
Beginner
37 Views

mecej4 wrote:

I have no experience with running Matlab on OSX, so my remarks may not apply.

Are you running a 32-bit version of Matlab, or a 64-bit version? How do you launch Matlab?

On Linux, I would suggest opening up a shell window, sourcing the Intel startup script, adding the Matlab BIN directory to PATH, adding the directory containing the Matlab shared libraries to LD_LIBRARY_PATH and then running the Matlab application from the same command window.

Please expand on why you think it is bad to have your Fortran application run as a post-processor. Are the data files being exchanged of very large size? How many times do you run the post-processor?

I am running a 64-bit version of Matlab on OSX. I just click the Matlab icon on the dashboard to launch it. Thank you for the shell window idea, I am going to give it a try.

Sorry maybe I was not clear: I don't think having Fortran application run as a post-processor is a bad idea. It is much faster than Matlab. The Matlab is controlling an ultrasound system which is collecting data with dimensions of about 200 x 200 x 100 in complex double format at a rate of about 20 times per second. So I have to transfer such 200 x 200 x 100 complex double matrix about 20 times a second between Matlab and Fortran. Ideally a gateway that supports fast data transfer between the two would be great. The data is in Matlab's workspace memory on the same Mac that runs the Fortran codes.

Thanks!

0 Kudos
Highlighted
Black Belt
37 Views

Good; one more question: what do you do with the results from the post-processing? In particular, do you pass them back to Matlab?

I think that you can slightly modify your Fortran code to produce a Mex file that can be called from Matlab with your 200 X 200 X 100 array as input.

0 Kudos
Highlighted
Beginner
37 Views

Yes I have to pass them back to Matlab for display, specifically overlaying the result on an existing Matlab figure.

Do you have an example of how to modify Fortran code to produce a mex file? So far what I could find is in this link: http://www.mathworks.com/help/matlab/matlab_external/create-fortran-source-mex-file.html. 

Thanks!

0 Kudos
Highlighted
Black Belt
37 Views

The example in the link that you provided, and several examples in the Matlab "extern" subdirectory on your computer, can be consulted.

You have to modify your Fortran source code such that the entry point is a subroutine called mexFunction, with input and output arguments in the way specified in the Matlab documentation.

0 Kudos