Fortran and matlab

1 次查看(过去 30 天)
Ahmed El-Ghandour
Ahmed El-Ghandour 2011-11-21
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

采纳的回答

Walter Roberson
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.

更多回答(2 个)

Ahmed El-Ghandour
Ahmed El-Ghandour 2011-11-21
Thanks a lot Walter for your fast reply,
my M file is called Training_M that is why I used it inthe fortran Code.
regarding nrhs=1, I want to pass "ix" so I belive that is why I need nrhs to be one ( number of inputs)
I have a nother question about where my files to be saved to be sure there will not be a prob in loading. Should the M file and Fortran files (in my work project ) to be in the same folder or not?
last point , when I run the code, I get the following message:
" Unable to start program ' C:\Ahmed\Work\training\Debug\Training.exe"
I do not know what is Training.exe. any suggestions? Ahmed
thanks a gain for your fast reply and your help
  2 个评论
Walter Roberson
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.
Ahmed El-Ghandour
Ahmed El-Ghandour 2011-11-21
I made the correction you mentioned...but now when I make the run...I have errors in the file: engCallMATLAB
I think I need to mention that the engCallMATLA is '.for' while I am working on intel fortran and my file is '.f90' is this a prob?

请先登录,再进行评论。


James Tursa
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 个评论
Ahmed El-Ghandour
Ahmed El-Ghandour 2011-11-29
Yes, I am using engCallMATLAB, and I did not use engOpen. so u think I should make my code as follow:
ep=engOpen(")
if(ep==0)then
write(*,*)'Engine did not open'
stop
endif
z=engCallMATLAB(ep, nlhs, plhs, nrhs, prhs,'Training_M')
I have 2 points not clear now: first, should I always include
include "fintrf.h"
include "engine.h"
if yes, I do not know how to do that and from where to get these files??
the second point, regarding the mwPointer, should I include:
mwPointer engCallMATLAB mwPointer engOpen
thanks a lot
Ahmed
James Tursa
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 CenterFile Exchange 中查找有关 Fortran with MATLAB 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by