如何用Matlab制作发布COM组件
1、选择 c/c++ 编译器

2、动部署工具,新建<generic COM Component>项目

3、 为COM组件添加新类及类的输出函数

4、M文件源码

5、 编译COM组件

6、完成编译

7、生成自解压安装包(本地使用的话,Matalb自动注册COM组件,无需发布; 但如果想要发布到没有安装Matlab的计算机上,则安装包中需添加Matab运行库 MCR,MCR安装一次即可,另外目标计算机上还需要有msjava.dll及VC运行库 msvcrt100.dll才能成功注册COM,系统环境缺失的常见dll文件可以用DirectX修复工具修复,或者在网上下载然后手动注册)

8、最终生成的自解压安装文件

9、部署到另一台没有Matlab的计算机上(例如Xp SP3,虚拟机也可)

10、利用VB6 引用COM组件进行实际开发(Delphi /VC / .Net 均可引用)

11、 VB6 测试 Demo 及源码
Option Base 1
Private comObj As CMCom '库中定义的类
Dim outZ As Variant '注意 Variant 数据类型
Dim inX(5) As Double
Dim inY(5) As Double
Private Sub Command1_Click()
'生成样本: 三次多项式 + 小扰动
For i = 1 To 5
inX(i) = i
inY(i) = i + 2 * i ^ 2 + 3 * i ^ 3 + 0.001 * Rnd(Now)
Next i
'调用 COM 组件成员函数
Call comObj.matlabPolyfit(1, outZ, inX, inY, 3)
'输出赝本数据
txtMsg.Text = txtMsg.Text & vbCrLf & "> 样本如下:" & vbCrLf & vbCrLf
txtMsg.Text = txtMsg.Text & " " & "x y" & vbCrLf
For i = 1 To 5
txtMsg.Text = txtMsg.Text & " " & inX(i) & ", " & inY(i) & vbCrLf
Next i
'输出拟合多项式
txtMsg.Text = txtMsg.Text & vbCrLf & "> 输出拟合系数:" & vbCrLf & vbCrLf & " Y = "
For i = 1 To 4
If i <> 4 Then
txtMsg.Text = txtMsg.Text & Format(outZ(1, i), "0.000") & " * X^" & (4 - i) & " + "
Else
txtMsg.Text = txtMsg.Text & Format(outZ(1, i), "0.000") & " * X^" & (4 - i)
End If
Next i
End Sub
Private Sub Form_Load()
Set comObj = New CMCom
End Sub
Private Sub Form_Unload(Cancel As Integer)
Set comObj = Nothing
End Sub
