vc只能调用matlab子函数,VC调用matlab函数

最近在学习matlab,先试一个VC调用matlab函数的简单例子

用的是VC++6.0,matlab7.8也就是matlabR2009

首先在matlab下设置环境

>> 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] Lcc-win32 C 2.4.1 in F:\MATLAB\R2009a\sys\lcc

[2] Microsoft Visual C++ 6.0 in F:\VC++6.0

[0] None

Compiler: 2

Please verify your choices:

Compiler: Microsoft Visual C++ 6.0

Location: F:\VC++6.0

Are these correct [y]/n?   y

Done . . .

>> 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] Lcc-win32 C 2.4.1 in F:\MATLAB\R2009a\sys\lcc

[2] Microsoft Visual C++ 6.0 in F:\VC++6.0

[0] None

Compiler: 2

Please verify your choices:

Compiler: Microsoft Visual C++ 6.0

Location: F:\VC++6.0

Are these correct [y]/n? y

Done . . .

**************************************************************************

**************************************************************************

1.然后设置系统路径。我的电脑->属性高级->环境变量->系统变量->Path选项,增加以下路径

C:\MATLAB7\extern\include:

C:\MATLAB\extern\lib\win32\microsoft\msvc60;

C:\MATLAB\bin\win32

ps: 使用时换成自己电脑上对应matlab的路径

2.做一个简单的M函数。文件名和函数名一致。

//文件名plot_test.m

function plot_test(result)

plot(result) %画图

3.在命令窗口敲deploytool(以前是comtool,在matlab7.8版本上没有这个命令),新建一个Comonent工程

怎么设置都在链接上看得到。

4. 单击Project Files->Add File 添加plot_test.m文件

5.单击Bulid,编译连接该组件。在你matlab工程文件夹下会有distrib和src两个子文件夹。在src下plot_idl_i.c,有关于com和

com接口GUID。其中CLSID在VC中需要用到

6.打开VC,创建一个单文档MFC工程文件。命令plot。创建两个menu消息,一个获得接口指针,另一个画图

7.打开类向导。在Add Class,选中From a typed library ,进入C:\MATLAB\work\plot\src文件夹,选中plot_idl.tlb

文件。

8.在CplotApp的InitInstance()中

BOOL suc = AfxOleInit();  //初始化OLE

if (suc == FALSE)

{

::AfxMessageBox("初始化OLE失败");

}

其次 在CplotView中包含plot_idl.h math.h头文件。并定义常量PI,并从src下plot_idl_i.c中拷贝类GUID并复制到plotView.h

文件中

#include "test_matlabDoc.h"

#include "test_matlabView.h"

#include "test_idl.h"

#include "math.h"

#define PI 3.14169265

const CLSID CLSID_Test = {0x37604F6E,0xE76B,0x4490,{0x8D,0x5D,0x3B,0x40,0xD1,0xF9,0xAD,0xE9}};

ITest plot;

//ps:文件名取名不同,换成自己对应的即可

void CTest_matlabView::OnDrawPic()

{

// TODO: Add your command handler code here

//画图

const int N = 100;

double a[N];

double f = 50;

double Ts = 0.002;

for (int i = 0; i < N; i++)

{

a[i] = sin(2*PI*f*i*Ts);

}

VARIANT x;

VariantInit(&x);

x.vt = VT_ARRAY|VT_R8;

SAFEARRAYBOUND rgsabound[1];

rgsabound[0].cElements = N;

rgsabound[0].lLbound = 0;

x.parray = SafeArrayCreate(VT_R8, 1, rgsabound);

SafeArrayLock(x.parray);

x.parray->pvData = a;

plot.plot_test(x);

SafeArrayUnlock(x.parray);

}

void CTest_matlabView::OnInitOle()

{

// TODO: Add your command handler code here

//初始化ole

plot.CreateDispatch(CLSID_Test, NULL);

COleDispatchDriver();

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值