calling matlab from fortran program

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

Antworten (1)

José-Luis
José-Luis am 4 Sep. 2012
Bearbeitet: José-Luis am 5 Sep. 2012

0 Stimmen

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 Kommentare

Mohammad Sadegh
Mohammad Sadegh am 5 Sep. 2012
Bearbeitet: Mohammad Sadegh am 5 Sep. 2012
Hi Jose Thank you so much for your replay,
Could you please tell me the steps I should do respectively(in more details)? As I understand I Should use the functions you mentioned in fortran,Is this right ? and what are the requirements for using Matlab Engine? What do you mean by "The tricky part (in my experience at least) is to link correctly to the matlab libraries" ?? Any help would be greatly appreciated.. Thanks
José-Luis
José-Luis am 6 Sep. 2012
Bearbeitet: José-Luis am 6 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
Mohammad Sadegh am 7 Sep. 2012
Thank you so much Jose I'm working on it!
Mohammad Sadegh
Mohammad Sadegh am 12 Sep. 2012
Bearbeitet: Mohammad Sadegh am 12 Sep. 2012
Hi again Jose I'm trying to do what you told for calling matlab from fortran, I started with the matlab example "fengdemo.F" to find out how to do my work. And I copied the example in Compaq visual fortran 6.6 and execute this.but when I execute this program in visual fortran the following errors disappear:
"Error: Syntax error, found ',' when expecting one of: ( : % . = => mwpointer engOpen, engGetVariable, mxCreateDoubleMatrix" Error: Syntax error, found END-OF-STATEMENT when expecting one of: ( : % . = => mwpointer mxGetPr"
what should I do now? :-(( could you please tell me a little more detail about how to do this Thank you a million !
José-Luis
José-Luis am 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
Mohammad Sadegh am 13 Sep. 2012
Bearbeitet: Mohammad Sadegh am 13 Sep. 2012
yes it is, I think the matlab engine functions should define for fortran, and "fintrf.h" defines the functions. but the problem is that fortran can't read this file,it seems that .h files is written for C code, not fortran I don't know what to do . How do you use MATLAB Engine ?
José-Luis
José-Luis am 13 Sep. 2012
Check if your compiler is supported. If not, try another one. For example for Matlab 2012a:

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Fortran with MATLAB finden Sie in Hilfe-Center und File Exchange

Produkte

Tags

Gefragt:

am 4 Sep. 2012

Community Treasure Hunt

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

Start Hunting!

Translated by