Fortran and matlab
4 views (last 30 days)
Show older comments
hey all;
I am working on Matlab FE code, and I am suppose to use a fortran code to call my matlab code during some processes.
I am trying to learn now using a simple matlab file ( just simple summation and multplying process), and I use the code: engCallMATLAB to call matlab.
my code ON FORTRAN is:
program Training
implicit none
! Variables
integer*8 ix,z,w,nrhs,nlhs, plhs,prhs,ep,engCallMATLAB
! Body of Training
ix=2
nrhs=1
nlhs=1
z=engCallMATLAB(ep, nlhs, plhs, nrhs, prhs,'Training_M')
w=z-2
print *, w
pause
end program Training
and my matlab simple code is:
clc;
clear all;
ix=2
y=ix^2+3*ix+1;
% u=getu(y);
z=3*y
simply, I need ix to be an input to the matlab code, and and to get z to fortran and then calculate w.
please I am so bad with fortran so if anyone can help I will be so gald.
thanks a lot
Ahmed
0 Comments
Accepted Answer
Walter Roberson
on 21 Nov 2011
Your Training_M.m file will need to contain a function named Training_M
Using a script file will not work properly.
You should also remove the "clc" and "clear all" commands
Your nrhs=1 declares that you are passing a variable in to the code, even though the code does not require any variables. But because you indicate that you are passing one, you will need to initialize prhs to contain an appropriate value. You will probably have to deal with plhs as well.
0 Comments
More Answers (2)
Ahmed El-Ghandour
on 21 Nov 2011
2 Comments
Walter Roberson
on 21 Nov 2011
Training.exe corresponds to what the Fortran compiler would generate as your Fortran source contains
Program Training
====
You will need to start your Training_M file with a "function" statement.
There should not be any problem with having your Fortran and MATLAB files in the same folder. Well, there _could_ be problems, but they are the sort of problems that usually take more work to solve than simply putting the files in to different folders.
James Tursa
on 21 Nov 2011
The engine will need to be opened first using engOpen to get the ep to pass to engCallMATLAB. I assume you are using my engCallMATLAB routine from the FEX since there is no such function in the regular MATLAB API. True? If not, you can find it here (along with an example):
For the .f90 question, you should edit the appropriate engopts.bat file and remove the /fixed option from the compiler options line. I would also do that for the mexopts.bat file as well, btw. That way .f90 files will automatically be interpreted as free form and .f files will be fixed form. As I recall, my engCallMATLAB code will work with either free or fixed form source files, but I will have to go check on that.
2 Comments
James Tursa
on 29 Nov 2011
Please post your entire sample code so that we can get everything corrected at once instead of piecemeal. Thanks. Do *NOT* include the engine.h file ... that is only for C/C++ apps. Instead, include all of the function type declarations that you use manually (or use my Fortran 95 interface package from the FEX).
See Also
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!