matcom进行matlab与c++混合编程

//先写一个简单的matlabhello.m

function hello() fprintf('hello,world/n'); end

 

在Mathtools的Mideva中选择File->Run将会生成matlabhello.h matlabhello.cpp matlabhello.lib matlabhello.dll等

//生成的matlabhello.h如下

#ifndef __matlabhello_h #define __matlabhello_h #include "matlib.h" Mm matlabhello(); #endif // __matlabhello_h

注意:#include "matlab.h"头文件需要手动添加

 

 

//生成的matlabhello.cpp如下

#include "StdAfx.h" #include "matlib.h" #pragma hdrstop #include "matlabhello.h" Mm matlabhello() { begin_scope #line 1 "c:/matcom45/matlabhello.m" call_stack_begin; #line 1 "c:/matcom45/matlabhello.m" // nargin, nargout entry code double old_nargin=nargin_val; if (!nargin_set) nargin_val=0.0; nargin_set=0; double old_nargout=nargout_val; if (!nargout_set) nargout_val=0.0; nargout_set=0; // translated code #line 2 "c:/matcom45/matlabhello.m" _ fprintf(TM("hello,world")); call_stack_end; // nargin, nargout exit code nargin_val=old_nargin; nargout_val=old_nargout; // function exit code return x_M; end_scope }

注意了,以上代码的头两行

#include "StdAfx.h" 
#include "matlib.h"

需要自己手动添加,否则会报错

 

下面的工作将是配置环节

将生成的matlabhello.h matlabhello.cpp拷贝到工程目录下,同时将这两个文件添加到工程中

注意混合编程还需要两个文件 matlib.h和v4501v.lib的支持,位置在C:/matcom45/lib下,将这两个文件拷贝

到工程目录下,将matlib.h添加到工程中来,将v4501v.lib添加到 工程->设置->Link下的Object/libary modules中来

 

下面调用生成的代码

#include "stdafx.h" #include "matlabhello.h" int main(int argc, char* argv[]) { initM(MATCOM_VERSION); matlabhello(); exitM(); return 0; }

 

此时一个简单的hello,world程序就做好了,希望对大家的混合编程有帮助

 

 

//以下为另外一个画图代码

void CExample2Dlg::OnButton1() { // TODO: Add your control notification handler code here double a1,a2; Mm P1,P2; UpdateData(TRUE); initM(MATCOM_VERSION); a1 = m_edit1; a2 = m_edit2; CWnd* p1 = NULL; p1 = (CWnd*)GetDlgItem(IDC_PIC); Mm plothandle = winaxes(p1->m_hWnd); //将static text句柄设置赋值给画图句柄 //vc调用matcom编译后的函数格式为f(argin1,argin2...,i_o,argout1,argout2,...) huatu(a1,a2,i_o,P1,P2); m_edit3.Format("%f",P1.r(1,1)); m_edit4.Format("%f",P2.r(1,1)); UpdateData(FALSE); exitM(); }

 

你可能感兴趣的:(编程,C++,c,工作,function,matlab)