【编者按】:网学网电气工程与自动化类别为您提供VB和MATLAB混合编程技术研究参考,解决您在VB和MATLAB混合编程技术研究学习中工作中的难题,参考学习。
客服咨询,网学网竭诚为您服务,本站永久域名:myeducs.cn | ||||||||||||||||||||||||||||||||||||||||||||||||||
4.1.2 OLE方法举例 下面的例子在服务器中执行MATLAB的version函数,并将输出返回到MATLAB客户程序中。 MATLAB作为客户程序时,使用下面的命令行。 h=actxserver(''MATLAB.application''); server_version =h.Execute(‘version’) server_version = ans = 7.0.1.24704 (R14) Service Pack 1 下面的例子用Visual Basic调用MATLAB进行多项式拟合计算和绘图。(多项式的拟合(Polynomial Fitting)又称为曲线拟合(Curve Fitting),其目的就是在众多的样本点中进行拟合,找出满足样本点分布的多项式。所用指令为polyfit,指令格式为:p=polyfit (x,y,n),其中x与y为样本点向量,n为所求多项式的阶数,p为求出的多项式。)首先在Visual Basic环境中设计窗体,如图3.1所示,窗体中各控件的属性设置如表4.1中所示: 图4.1 设计对话框 表4.1 各控件的属性设置
在窗体中输入下面的代码,利用VB前端界面中输入的数据参数,调用MATLAB进行计算和绘图。 Option Explicit ''将MATLAB实例对象定义为公共变量 Public objMATLAB As Object ''定义一个实现计算或绘图的过程 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 i As Integer Dim strCommand As String intNum = Val(Text1.Text) intLevel = Val(Text2.Text) Open App.Path + "/datX" For Output As 1 Print #1, Text3 Close 1 Open App.Path + "/datY" For Output As 1 Print #1, Text4 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中要执行的命令 strCommand = "n=" & Str(intLevel) & ";x=[" For i = 1 To intNum strCommand = strCommand & Str(x(i)) & "" Next i strCommand = strCommand & "];y=[" For i = 1 To intNum strCommand = strCommand & Str(y(i)) & "" Next i strCommand = strCommand & "];" If CorP Then strCommand = strCommand & "polyfit(x,y,n)" Text5 = objMATLAB.execute(strCommand) Else strCommand = strCommand & "plot(x,y)" objMATLAB.execute (strCommand) End If End Sub | ||||||||||||||||||||||||||||||||||||||||||||||||||
本站发布的计算机毕业设计均是完整无错的全套作品,包含开题报告+程序+论文+源代码+翻译+答辩稿PPT | ||||||||||||||||||||||||||||||||||||||||||||||||||
本文选自计算机毕业设计http://myeducs.cn |