搜档网
当前位置:搜档网 › VB调用MATLAB步骤

VB调用MATLAB步骤

必备:
matlab7.0
vc++6.0
vb6.0
1. 在matlab的command window里面敲入mex –setup
2. Please choose your compiler for building external interface (MEX) files: Would you like mex to locate installed compilers [y]/n?选择n
3.matlab会列出所有电脑上所有的编译器,选择Microsoft Visual C/C++ version 6.0
4.The default location for Microsoft Visual C/C++ compilers is C:\Program Files\Microsoft Visual Studio, but that directory does not exist on this machine
Use C:\Program Files\Microsoft Visual Studio anyway [y]/n? 选择n
5.Please enter the location of your compiler: [C:\Program Files\Microsoft Visual Studio] 键入vc++6的安装位置E:\INSTALLBACK\Microsoft Visual Studio vc6.0
6.确定y,Done...

7.设置matlab编译器,在matlab的command window里面敲入mbuild –setup
8.同上步骤选择vc6

9.在matlab的command window里面敲入comtool
10.新建一个工程abc,component name 为abc,class name 为ab,OK
11.选择ab,点击Add file,选择m文件
12.Build->Com object
13.standard dll succeed
注意:若改变M文件,则重新执行步骤12
14.打开VB,工程-》引用,选择abc1.0type library

MATLAB程序如下:
function Y=myfunction(i,j) %myfunction为函数名,a,b为输入变量,Y为输出变量
Y=a+b;

VB程序如下:
Private Sub Command1_Click()
Dim hh As New ABC.ab 'ABC 为COMPONENT NAME,ab为class name
Dim a As Double ‘输入参数
Dim b As Double ‘输入参数
Dim c As Variant ‘输出参数
a = Val(Text1.Text)
b = Val(Text2.Text)
Call hh.myadd(1, c, a, b) 'myadd为MATLAB中的函数名,1为输出变量个数,c为输出变量定义为variant型,a,b为输入变量,定义为double型
Text3.Text = c
End Sub


相关主题