正文

VB与MATLAB通信实现方案一:OLE2007-07-19 18:38:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/emath/27621.html

分享到:


 

            VBMATLAB通信实现方案一

 

前言:MATLABVB接口,有使用DLL, DDE ,OLE ,ActiveX ,COM 组件等多种方法.

本文介绍的方法:

   在VB中通过调用MATLAB的引擎(Engine)接口,使用Windows的OLE或ActiveX通道与MATLAB通信。通过Object.ExecuteMATLAB语句在程序中直接使用MATLAB提供的函数库和图形库。这种方法可以较快地编写出程序;但在调用引擎时,会在后台启动一个MATLAB进程,影响程序运行速度,占用内存。


 

例子: VB调用MATLAB进行多项式拟合

MATLAB中进行多项式拟合的库函数:

polyfit(x,y,n) 其中x,y为拟合数据,n为拟合多项式的阶数。

:用最小二乘法拟合数据

   x:   0.50   1.00   1.50    2.00     2.50   3.00

   y:  1.75   2.45   3.81   4.80     8.00   8.60

 x=[0.5,1,1.5,2,2.5,3]     y=[1.75,2.45,3.81,4.80,8.00,8.60]

 a=polyfit(x,y,2) %2次多项式拟合上组数据,a为拟合多项式的系数向量

x1=0.5:0.05:3

 y1=a(1)*x1.^2+a(2)*x1+a(3)

 plot(x1,y1) %画出拟合曲线的图形

 hold on  %保留上面的图形和坐标,可在该坐标系中继续作图

 plot(x,y,*) %*号的形式画出被拟合的数据图形

 

VB调用MATLAB进行多项式拟合:

建立VB工程,界面如下:

 

VB代码:

Option Explicit

Public objmatlab As Object 'MATLAB实例对象定义为公共变量

 

Private Sub computeorplot(corp As Boolean)

   Dim intnum As Integer

   Dim intlevel As Integer

  

   Dim x(1 To 100) As Double

   Dim y(1 To 100) As Double

   Dim strmodel As String

   Dim strcom, strcom1, p As String

   Dim i As Integer

  

   intnum = Val(Text1.Text) '数据数组

   intlevel = Val(Combo1.Text) '多项式阶数

  

   Open App.Path + "/datx" For Output As 1

       Print #1, Text3  'X数组

       Close 1

   Open App.Path + "/daty" For Output As 1

       Print #1, Text4  'Y数组

       Close 1

      

   Open App.Path + "/datx" For Input As 1

       For i = 1 To intnum

           Input #1, x(i)

           Next i

           Close 1

      

  Open App.Path + "/daty" For Input As 1

       For i = 1 To intnum

           Input #1, y(i)

           Next i

           Close 1

   '定义在MATLAB中要执行的命令

   strcom = "n=" & Str(intlevel) & "; x=["

     For i = 1 To intnum

        strcom = strcom & Str(x(i)) & ""

       Next i

      

    strcom = strcom & "]; y=["

     For i = 1 To intnum

        strcom = strcom & Str(y(i)) & ""

       Next i

      

    strcom = strcom & " ];"

      If corp Then

        strcom = strcom & "p=" & "polyfit(x,y,n)"

        Text5 = objmatlab.execute(strcom)

        strcom1 = strcom1 & "y=" & "poly2str(p,'*x')"

        Text2 = objmatlab.execute(strcom1)

       

       Else

        strcom = strcom & "plot(x,y)"

        objmatlab.execute (strcom)

        End If

  

End Sub

 

Private Sub Command1_Click() '计算

Dim bolcorp As Boolean

bolcorp = True '进行多项式拟合

Call computeorplot(bolcorp)

 

End Sub

 

Private Sub Command2_Click() '绘图

Dim bolcorp As Boolean

bolcorp = False '绘图

Call computeorplot(bolcorp)

 

End Sub

 

Private Sub Command3_Click() '退出

Set objmatlab = Nothing

Unload Me

End Sub

 

Private Sub Command4_Click() '取消

Text1.Text = ""

Combo1.Text = ""

Text3.Text = ""

Text4.Text = ""

Text5.Text = ""

End Sub

 

Private Sub Form_Initialize()

Set objmatlab = CreateObject("matlab.application") '创建MATLAB实例

 

End Sub

按下绘图后执行结果:


 

补充说明:

1.       要在工程中引用:MATLAB Application

2.       使用CreateObject函数创建Matlab对象一般格式:

Dim objmatlab As Object

 Set objmatlab = CreateObject("matlab.application")

3.       自动化之Execute方法:

Execute方法将一个命令字符串作为变量并返回一个字符串

‘Dim strcom  As String

Text1 = objmatlab.execute(strcom)

 

Edited by Guassfans 2007-07-19

阅读(5876) | 评论(0)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

暂无评论
您需要登录后才能评论,请 登录 或者 注册