posts - 112, comments - 215, trackbacks - 0, articles - 34
  IT博客 :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

VC调用matlab中定义的.m文件中的函数的实例

Posted on 2006-12-18 14:24 济公 阅读(16992) 评论(21)  编辑 收藏 引用 所属分类: VCMatlab

Matlab是一个强大的数学计算/仿真工具,其内置了很多实用的现成的函数,而且我们经常也自己定义很多m函数。但在很多情况下,我们不得不使用VC编程。那么,如何在VC中利用matlab的资源呢?在这里我简要的以一个简单的例子来说明一下如果在VC中调用matlab中定义的.m文件。繁多的理论就不说了,简明扼要的说一个实例。相信大家看过之后都会马上学会的J

            其中灰色底显示的部分是需要我们手工输入的部分。

            步骤如下:

            ㈠当然是先建立 matlab m 文件。

这里为了简单起见,我在matlab中定义了一个函数,m文件如下:

function y = add_zh(a,b)     

y=a+b

保存为myfunct.m

            ㈡下面来对 matlab 编译环境进行设置。 打开matlab,在命令行状态下键入:mex-setup

然后出现如下提示:

--------------------------------------------------------------------------------------

Select a compiler:

[1] Borland C++Builder version 5.0 in C:\Program Files\Borland

[2] Lcc C version 2.4 in F:\SOFT\SHARE\MATLAB65\sys\lcc

[3] Microsoft Visual C/C++ version 6.0 in C:\Program Files\Microsoft Visual Studio

[0] None

 

Compiler:

--------------------------------------------------------------------------------------

这里我们选择’3’。(如果使用其他的编译器比如BCB,那么就可以相应的选择’1’

然后,继续在命令行状态下输入:

mbuild –setup

--------------------------------------------------------------------------------------

Please choose your compiler for building standalone MATLAB applications: 

Would you like mbuild to locate installed compilers [y]/n? y

Select a compiler:

[1] Borland C++Builder version 5.0 in C:\Program Files\Borland

[2] Lcc C version 2.4 in F:\SOFT\SHARE\MATLAB65\sys\lcc

[3] Microsoft Visual C/C++ version 6.0 in C:\Program Files\Microsoft Visual Studio

[0] None

 

Compiler:

--------------------------------------------------------------------------------------

这里需要几步确认。大家按情况操作即可。

            ㈢对 VC 的编译器环境进行设置。 Tools-options-directory里面设置includelib的路径,分别添加一项,指向matlab安装目录中的\extern\include \extern\lib

            ㈣这一步比较关键。在这一步中,将 matlab 中的 .m 文件转换为 dll

matlab命令行中输入:

mcc -t -h -L C -W lib:ppp -T link:lib myfunct.m

         至此,可以发现在myfunct.m所在的目录中产生了一大堆文件。我们只用其中3个:

            ppp.h    ppp.lib     ppp.dll

          ㈤新建一个 VC 工程 。这里取名为 mat 。为了简单,我们在这里建立一个对话框来进行演示。如下:

然后,将上面这3个文件拷贝到VC工程所在的目录中,以便VC调用之。

            ㈥进行 VC 编程。

首先,在cpp文件中加入:  #include “ppp.h”

然后,设置VC的链接库。在project-settings-link下的object/library modules中加入:

ppp.lib libmx.lib libmatlb.lib libmmfile.lib

至此,VC的编译环境设置完毕。下面就是编程部分了。

            ㈦编程部分的关键:接口和数据类型的转换

先给出 VC 实现的源代码:

------------------------------------------------------------------------

void CMatDlg::OnButton1()

{           // matlab 中定义的 m 文件 :myfunct ,其功能是完成两个数的相加操作。

                CString str;

            UpdateData(true);

            pppInitialize();

            // 为了调用 matlab 中的函数,必须使用数组数据类型,并其后调用 matlab 函数将其转化为矩阵格式( matlab 的基本数据类型是矩阵)

            static double x1[1]={1.0};

            static double x2[1]={2.5};

            double result;

            // 调用 matlab 创建 3 个矩阵

            mxArray *A=mclGetUninitializedArray();

            mxArray *B=mclGetUninitializedArray();

            mxArray *C=mclGetUninitializedArray();

            // C 语言中的变量值赋给 matlab 中的矩阵

            mlfAssign(&A,mlfDoubleMatrix(1,1,x1,NULL));

            mlfAssign(&B,mlfDoubleMatrix(1,1,x2,NULL));

             mlfAssign(&C,mlfMyfunct(A,B));

            // matlab 中的矩阵的指针传递给 C 语言中的指向 double 的指针

            double * md=mxGetPr(C);

            result=md[0];

            // 释放这些矩阵

            mxDestroyArray(A);

            mxDestroyArray(B);

            mxDestroyArray(C);

            str.Format ("It is : %f",result);

            ::AfxMessageBox(str);

}

------------------------------------------------------------------------

相信看到里面的注释,我们就明白的差不多了。

VC 编程以实现对 matlab 函数的调用,例如 VC 中我们实现两个 double 型的相加,则需要做上述的编程:首先定义两个数组并存入我们要进行函数操作的数据(也就是所谓的实参);然后将这两个数组转换为 matlab 可以识别的矩阵;然后调用 m 文件中自定义的函数,对矩阵进行操作;最后将结果再转回 VC 支持的数据格式:

            double * md=mxGetPr(C);

            result=md[0];      

至此,经 matlab 函数处理过的数据已经存入了 VC 中的 result 变量。         

 

            mlfAssign(&C,mlfMyfunct(A,B));

大家可以看出来,在进行调用的时候,我们使用的是 m 文件的文件名 myfunct ,而不是 m 文件中定义的 add_zh(a,b)

Feedback

# re: VC调用matlab中定义的.m文件中的函数的实例  回复  更多评论   

2007-03-17 20:13 by 懒汉
简单明了

# re: VC调用matlab中定义的.m文件中的函数的实例  回复  更多评论   

2007-05-14 01:54 by Favory
兄弟请问一下,我用的是matlab7.1的为什么编译不通过呀?
Compiling resources...
Compiling...
StdAfx.cpp
Compiling...
add.cpp
addDlg.cpp
E:\try\add\addDlg.cpp(195) : error C2065: 'mclGetUninitializedArray' : undeclared identifier
E:\try\add\addDlg.cpp(195) : error C2440: 'initializing' : cannot convert from 'int' to 'struct mxArray_tag *'
Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
E:\try\add\addDlg.cpp(197) : error C2440: 'initializing' : cannot convert from 'int' to 'struct mxArray_tag *'
Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
E:\try\add\addDlg.cpp(199) : error C2440: 'initializing' : cannot convert from 'int' to 'struct mxArray_tag *'
Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
E:\try\add\addDlg.cpp(203) : error C2065: 'mlfAssign' : undeclared identifier
E:\try\add\addDlg.cpp(203) : error C2065: 'mlfDoubleMatrix' : undeclared identifier
E:\try\add\addDlg.cpp(207) : error C2660: 'mlfAdd_zh' : function does not take 2 parameters
Generating Code...

# re: VC调用matlab中定义的.m文件中的函数的实例  回复  更多评论   

2007-05-14 22:17 by 济公
To:Favory
你在6.5下做过吗?

# re: VC调用matlab中定义的.m文件中的函数的实例  回复  更多评论   

2007-05-15 17:19 by Favory
6.5下应该没问题得,谢谢兄弟的文章!

# re: VC调用matlab中定义的.m文件中的函数的实例  回复  更多评论   

2007-05-15 18:35 by 济公
TO:Favory
我想应该是matlab7.1和6.5的接口函数有点差别,你可以查看一下帮助,或者Maltab下的一些头文件看看,或者搜索看看有没有7.1的实例。

# re: VC调用matlab中定义的.m文件中的函数的实例  回复  更多评论   

2007-05-17 15:50 by 树叶
为什么,按着文章写的作,然后在编译的时候不能打开libmx.lib libmatlb.lib libmmfile.lib 这些文件阿
要怎么解决阿,我已经设置了编译器了
然后在C程序编译时就出现了LINK : fatal error LNK1104: cannot open file "libmx.lib"
Error executing link.exe.
这要怎么解决啊?

# re: VC调用matlab中定义的.m文件中的函数的实例  回复  更多评论   

2007-11-03 21:56 by hungry
这是我见过写的最清楚的了,好极!!!!!!!!

# re: VC调用matlab中定义的.m文件中的函数的实例  回复  更多评论   

2008-07-04 20:52 by chenjunjiang
楼主,按照你的方法是挺好用,但是我在使用过程中遇到个新的问题,我的VC程序需要循环调用同一个MATLAB的DLL库里的函数,每次我都是第一次调用的时候是正确的,但是我第二次调用是就出现非法的错误,能请教如何解决么?谢谢。我的email:junjiang_chen@hotmail.com,或者junjiang.chen@yahoo.com

# re: VC调用matlab中定义的.m文件中的函数的实例  回复  更多评论   

2008-11-04 11:50 by foreverlml
楼主您好:
我的m文件中又调用了另一个m文件的函数,那么我现在想在vc中使用第一个m文件的函数,应该怎么做啊?谢谢~~!!我的email:foreverlml@163.com或者

# re: VC调用matlab中定义的.m文件中的函数的实例  回复  更多评论   

2009-04-24 17:39 by bcmd
请问如果选complier的时候选错了该怎么撤消操作啊。。。。。

# re: VC调用matlab中定义的.m文件中的函数的实例  回复  更多评论   

2009-08-01 08:23 by mmm
前六步都已经完成并且没有错误,但是不知道第7步给出的源代码放在哪个文件中的什么位置?

# re: VC调用matlab中定义的.m文件中的函数的实例  回复  更多评论   

2009-09-17 13:02 by 李群
楼主,您好!我用的matlab7.4与vc++6.0混合编程,.m文件是显示一个sin函数的图像,在Vc中弄好之后,编译通过了,但是在调用matlab中生成的.mexw32文件时,却出现不能编辑mexw32文件。如果单独打开matlab中的.exe文件却可以看到sin图像。问题出在哪里呢?还有,在mexFunction中应该编写什么样的程序呢?

# re: VC调用matlab中定义的.m文件中的函数的实例  回复  更多评论   

2009-09-17 15:26 by 李群
楼主,您怎么不回贴呢?我按照上面的方法试了,行不通啊!

# re: VC调用matlab中定义的.m文件中的函数的实例[未登录]  回复  更多评论   

2010-10-06 14:15 by w
路径问题。我直接把这几个文件拷贝到当前路径下就好了

# re: VC调用matlab中定义的.m文件中的函数的实例  回复  更多评论   

2011-04-27 17:14 by 网友
lz您好!
我在matlab 6.5下输入mex -setup.选择“y”后,只出现了LCC编译器,我的机器上安装了VC。但是没有显示出VC。怎么回事呢?
谢谢!!!

# re: VC调用matlab中定义的.m文件中的函数的实例  回复  更多评论   

2014-04-03 15:40 by 111
@Favory
我的错误跟你的一样,你解决了吗?

# re: VC调用matlab中定义的.m文件中的函数的实例  回复  更多评论   

2014-11-11 15:49 by 陈大可
@Favory
你的问题解决了吗 我也是的
C:\Users\Administrator\Desktop\Mat\MatDlg.cpp(187) : error C2065: 'mclGetUninitializedArray' : undeclared identifier
C:\Users\Administrator\Desktop\Mat\MatDlg.cpp(187) : error C2440: 'initializing' : cannot convert from 'int' to 'struct mxArray_tag *'
Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
C:\Users\Administrator\Desktop\Mat\MatDlg.cpp(188) : error C2440: 'initializing' : cannot convert from 'int' to 'struct mxArray_tag *'
Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
C:\Users\Administrator\Desktop\Mat\MatDlg.cpp(189) : error C2440: 'initializing' : cannot convert from 'int' to 'struct mxArray_tag *'
Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
C:\Users\Administrator\Desktop\Mat\MatDlg.cpp(192) : error C2065: 'mlfAssign' : undeclared identifier
C:\Users\Administrator\Desktop\Mat\MatDlg.cpp(192) : error C2065: 'mlfDoubleMatrix' : undeclared identifier
C:\Users\Administrator\Desktop\Mat\MatDlg.cpp(194) : error C2660: 'mlfMyfunction' : function does not take 2 parameters

# re: VC调用matlab中定义的.m文件中的函数的实例  回复  更多评论   

2015-03-12 00:31 by 大小姐pf
d:\新建文件夹 (2)\asa\asadlg.cpp(4) : fatal error C1083: Cannot open precompiled header file: 'Debug/asa.pch': No such file or directory
这是什么情况
只有注册用户登录后才能发表评论。