300字范文,内容丰富有趣,生活中的好帮手!
300字范文 > vc只能调用matlab子函数 VC调用matlab函数

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

时间:2023-04-18 11:16:10

相关推荐

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

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

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

首先在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\Ra\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\Ra\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();

}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。