Fortran and matlab
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Walter Roberson
2011-11-21
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 个评论
更多回答(2 个)
Ahmed El-Ghandour
2011-11-21
2 个评论
Walter Roberson
2011-11-21
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
2011-11-21
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 个评论
James Tursa
2011-11-29
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).
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Fortran with MATLAB 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!