http://blog.sina.com.cn/s/blog_476687050100cs24.html
(这段时间写文章去了,把Fortran与Matlab的交互搁置了一段时间,现在再重新开始摸索。)
首先要说如何配置,使得Fortran中可以使用Matlab,或Matlab中可使用Visual Fortran。
软件版本:Compaq Visual Fortran 6.6 (CVF 6.6)和Matlab 7.0.4 (个人信息)
安装路径:CVF 6.6:D:\Program Files\Microsoft Visual Studio
Matlab: D:\Program Files\MATLAB704
第一步:安装
首先在Matlab中运行命令 “mex -setup”,会出现
Please choose your compiler for building external interface (MEX) files:
Would you like mex to locate installed compilers [y]/n?
输入 ‘y' 之后,出现
Select a compiler:
[1] Compaq Visual Fortran version 6.6 in D:\Program Files\Microsoft Visual Studio
[2] Lcc C version 2.4 in D:\PROGRAM FILES\MATLAB704\sys\lcc
[3] Microsoft Visual C/C++ version 6.0 in D:\Program Files\Microsoft Visual Studio
[0] None
Compiler:
在“Compiler:”后面选1即可。
第二步:配置编译环境
配置编译环境时可有两种选择
1)Matlab下编译 (为简单,假定Matlab的安装目录为%Matlab%)
首先打开 %Matlab%\bin\win32\mexopts\df60engmatopts.bat
将13行中的 set DF_ROOT=%DF_ROOT%
改成
set DF_ROOT=%CVF 6.6% (CVF的安装目录)
将Matlab的工作目录改成泥的fortran源代码存放的目录,最后在Matlab中运行:
mex -f df60engmatopts.bat example.for
(假设源文件为example.for)
2)CVF下编译
首先新建一个项目,然后再tools-->directory里设置include和lib目录,主要是要添加:
include ->D:\Program Files\MATLAB704\extern\include (注意个人Matlab的安装情况)
然后在lib中
lib -> D:\Program Files\MATLAB704\extern\lib\win32
lib -> D:\Program Files\MATLAB704\extern\lib\win32\digital\df60
最后在project ->setting ->link ->Object/Library modules里添加
libmx.lib libmex.lib libmat.lib libeng.lib
至此,配置过程完成。下面可利用程序进行测试。
%==============================================
integer engOpen,engGetVariable,mxCreateDoubleMatrix
integer mxGetPr
integer ep,T,D
double precision time(10),dist(10)
integer engPutVariable,engEvalString,engClose
integer temp,status
data time /1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0,10.0/
ep=engOpen('matlab')
if(ep.eq.0) then
write(6,*) '!Can't strat Matlab !'
stop
endif
T=mxCreateDoubleMatrix(1,10,0)
call mxCopyReal8ToPtr(time,mxGetPr(T),10)
status=engPutVariable(ep,'T',T)
if(status.ne.0) then
write(6,*) 'engPutVariable failed'
stop endif
if (engEvalString(ep,'D=.5.*(-9.8).*T.^2;').ne.0) then
write(6,*) 'engEvalString failed'
stop
endif
if (engEvalString(ep,'plot(T,D);').ne.0) then
write(6,*) 'engEvalString failed'
stop
endif
if (engEvalString(ep,'slabel("Time(seconds)")').ne.0) then
write(6,*) 'engEcalString failed'
stop
endif
print *, 'Type 0 <return> to Exit'
print *, 'Type 1 <return> to continue'
read (*,*) temp
if (temp.eq.0) then
print *, 'EXIT'
stop
endif
if (engEvalString(ep,'close;').ne.0) then
write(6,*) 'engEvalString failed'
stop
endif
D=engGetVariable(ep,'D')
call mxCopyPtrToReal8(mxGetPr(D),dist,10)
print *,'Matlab comput:'
print *,'times(s) distance(m)'
do 10 i=1,10
print 20, time(i),dist(i)
20 format(' ',G10.3,G10.3)
10 continue
call mxDestroyArray(T)
call mxDestroyArray(D)
status=engClose(ep)
if (status.ne.0) then
write(6,*) 'engClose failed'
stop
endif
stop
end