常见问题 教学软件的常见问题>>

).如何在Excel中嵌入Matlab?

发布时间:2010-11-24 13:32:37

答案:

如果你的Matlab安装有ExcelLink,它可以实现Excel与Matlab直接的数
        据交换,可以在Excel中直接调用matlab的函数,进行绘图或者数据处理。

        不过如果没有安装ExcelLink,你仍然可以使用Matlab的ActiveX接口来
        调用matlab,下面是一个Excel宏函数,作为例子:
       
#Brett Shoelson(bshoelson@cox.rr.com),2001/11/01,comp.soft-sys.matlab#

        Sub CallMatlab()
        ' Dimension variables
        Dim MatLab As Object
        Dim Result
        Dim Invals(3, 4) As Double
        Dim MImag() As Double
        Dim i, j As Integer
        ' Invoke Matlab
        Set MatLab = CreateObject("Matlab.Application")
        ' Read Invals from current spreadsheet
        ' (Assume Invals stored in B3:E5)
        For i = 0 To 2
        For j = 0 To 3
        Invals(i, j) = ActiveSheet.Range(Cells(i+3,j+2),
          Cells(i+3,j+2)).Value
        Next j
        Next i
        ' Send Invals to Matlab
        Call MatLab.PutFullMatrix("a", "base", Invals, MImag)
        ' Send instructions to Matlab
        Result = MatLab.Execute("b=a.^2;")
        ' Retrieve Result
        Call MatLab.GetFullMatrix("b", "base", Invals, MImag)
        ' Store Result in B8:E10
        ActiveSheet.Range("B8:E10").Value = Invals
        End Sub

 

本FAQ适用范围

下一步您可以:
查看教学软件产品 >>