calling matlab from fortran program
Show older comments
Hello I have a code in FORTRAN and in part of it I need to carry out some computations with MATLAB. I've written my auxiliary code in MATLAB and Simulink and I want to get a variable(for example "Sens") from FORTRAN and give it to MATLAB, then executing MATLAB code that computes target variable(for example "Amp"),and send "Amp" variable to FORTRAN for running the rest of the main code. In fact I need to call my MATLAB code from FORTRAN. I don't have much skill in FORTRAN but I know MATLAB and Simulink good. I've found in MATLAB central that it is possible to call MATLAB from FORTRAN with MATLAB Engine Functions , Can you please advise me if my work is possible with MATLAB Engine functions and if, how can I do that? I looked into MATLAB documentation about "Calling MATLAB from a Fortran Application", but I didn't understand exactly what to do and this is what I need or not. And is there any easiest way to do my work. Thank you very much
Answers (1)
Yes, it is possible.
I guess, the functions you want to look at are:
Create matlab (numeric array) array:
mxCreateNumericArray
Getting a pointer to the start of the Matlab array so you can populate it with your own data:
mxGetPr
Then you can start a matlab engine:
engOpen
Place your variable in the engine:
engPutVariable
Peform your calculations:
engEvalString
Get your results back (if you don't save to the same variable):
engGetVariable
Close the engine:
engClose
Copy your variable back to FORTRAN if necessary. Deallocate matlab arrays:
mxDestroyArray
Get on with your code... The tricky part (in my experience at least) is to link correctly to the matlab libraries, with the usual fun related to 32bit vs 64bit, different Matlab versions installed, linux vs windows, etc...
Cheers!
7 Comments
Mohammad Sadegh
on 5 Sep 2012
Edited: Mohammad Sadegh
on 5 Sep 2012
It all depends on what you want to do:
Use Fortran code in matlab: mex files
Use matlab code in fortran: matlab engine
A good place to start is the documentation of the C++/Fortran Api.
You will find quite a few examples in there. For instance an example of using the Matlab engine in FORTRAN (run the following in Matlab's command line).
edit([matlabroot '/extern/examples/eng_mat/fengdemo.F'])
If you want to use the engine; how easy, or how complicated will depend on how familiar you are writing, compiling and linking FORTRAN code. But there is no magic formula. If you are unfamiliar with all this, work your way from the (relatively simple) examples that you can find in the documentation.
Mohammad Sadegh
on 7 Sep 2012
Mohammad Sadegh
on 12 Sep 2012
Edited: Mohammad Sadegh
on 12 Sep 2012
José-Luis
on 12 Sep 2012
I have never used Visual Fortran. The only thing that occurs to me is: do you have your paths set correctly? Is "fintrf.h" in your search path.
Mohammad Sadegh
on 13 Sep 2012
Edited: Mohammad Sadegh
on 13 Sep 2012
José-Luis
on 13 Sep 2012
Check if your compiler is supported. If not, try another one. For example for Matlab 2012a:
Categories
Find more on Fortran with MATLAB in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!