Fortran调用Matlab
2011年12月27日
【试验环境】Windows 2003 server std,matlab r2007b,MinGW gcc 4.2 (sjlj)。Visual Studio 2005
【试验任务】实现在fortran程序中调用matlab编译生成的DLL。
【试验步骤】首先,创建一个简单的m代码,这里用拙作《深入浅出matlab7.x混合编程》中用到的mcctest01.m作为例子,它是一个很简单的m方程,程序代码如下:
代码:function [b]mcctest01
[/b]% MCCTEST01 Create some data and draw a 2D x-y plot
% mcctest01 is used to demonstrates how to convert M-functions into
% library files and how to use created library files with MFC
% applications.
%
% Inputs:
% None
%
% Output:
% None
%
% Copyright (c) 2005 Dong Weiguo
% create data
x = 0:.01:20;
y = x.*sin(x);
% draw a plot
plot(x,y);
xlabel('x');
ylabel('y');
然后在matlab中编译mcctest01,生成[b]libmcctest01.dll[/b]。注意,如果这是你第一次在matlab中使用mcc命令,你需要先运行mbuild -setup来配置。我使用Visual Studio 2005编译器。在matlab中执行如下命令:
[b]mcc -W cpplib:libmcctest01 -T link:lib mcctest01.m[/b]
这里假设matlab
2011年12月27日
【试验环境】Windows 2003 server std,matlab r2007b,MinGW gcc 4.2 (sjlj)。Visual Studio 2005
【试验任务】实现在fortran程序中调用matlab编译生成的DLL。
【试验步骤】首先,创建一个简单的m代码,这里用拙作《深入浅出matlab7.x混合编程》中用到的mcctest01.m作为例子,它是一个很简单的m方程,程序代码如下:
代码:function [b]mcctest01
[/b]% MCCTEST01 Create some data and draw a 2D x-y plot
% mcctest01 is used to demonstrates how to convert M-functions into
% library files and how to use created library files with MFC
% applications.
%
% Inputs:
% None
%
% Output:
% None
%
% Copyright (c) 2005 Dong Weiguo
% create data
x = 0:.01:20;
y = x.*sin(x);
% draw a plot
plot(x,y);
xlabel('x');
ylabel('y');
然后在matlab中编译mcctest01,生成[b]libmcctest01.dll[/b]。注意,如果这是你第一次在matlab中使用mcc命令,你需要先运行mbuild -setup来配置。我使用Visual Studio 2005编译器。在matlab中执行如下命令:
[b]mcc -W cpplib:libmcctest01 -T link:lib mcctest01.m[/b]
这里假设matlab

本文介绍了如何在Fortran程序中调用由Matlab编译生成的DLL。首先在Matlab中编译M文件,然后将生成的DLL、库文件和头文件复制到Fortran源代码目录。由于Fortran不能直接使用DLL,因此需要通过C或C++的wrapper函数间接调用。文章提供了一个C++的wrapper函数示例,并展示了简单的Fortran主程序调用该wrapper函数的方法。
最低0.47元/天 解锁文章
1254

被折叠的 条评论
为什么被折叠?



