External Interfaces |
Examples of a MATLAB Automation Server
This section provides code examples that show you how to access a MATLAB automation server from Visual Basic and C#.
Example -- Running an M-File from Visual Basic
This example calls an M-file function named solve_bvp
from a Visual Basic client application through a COM interface. It also plots a graph in a new MATLAB window and performs a simple computation:
Dim MatLab As Object Dim Result As String Dim MReal(1, 3) As Double Dim MImag(1, 3) As Double Set MatLab = CreateObject("Matlab.Application") 'Calling m-file from VB `Assuming solve_bvp exists at specified location Result = MatLab.Execute("cd d:\matlab\work\bvp") Result = MatLab.Execute("solve_bvp") 'Executing other MATLAB commands Result = MatLab.Execute("surf(peaks)") Result = MatLab.Execute("a = [1 2 3 4; 5 6 7 8]") Result = MatLab.Execute("b = a + a ") `Bring matrix b into VB program Call MatLab.GetFullMatrix("b", "base", MReal, MImag)
Example -- Viewing Methods from a Visual Basic 6.0 Client
You can find out what methods are available from a MATLAB automation server using the Object Browser of your Visual Basic client application. To do this, follow this procedure in the client application to reference the MATLAB Application Type Library:
This enables you to view MATLAB automation methods from the Visual Basic Object Browser under the Library called MLAPP
. You will now also be able to see a list of MATLAB automation methods when you use the term Matlab
followed by a period. For example,
Dim Matlab As MLApp.MLApp Private Sub View_Methods() Set Matlab = New MLApp.MLApp 'The next line should show a list of MATLAB Automation methods Matlab. End Sub
Example -- Calling MATLAB from a C# Client
This example simply creates data in the client C# program and passes it to MATLAB. The matrix (containing complex data) is then passed back to the C# program.
Note that the reference to the MATLAB Type Library for C# is:
using System; namespace ConsoleApplication4 { class Class1 { [STAThread] static void Main(string[] args) { MLApp.MLAppClass matlab = new MLApp.MLAppClass(); System.Array pr = new double[4]; pr.SetValue(11,0); pr.SetValue(12,1); pr.SetValue(13,2); pr.SetValue(14,3); System.Array pi = new double[4]; pi.SetValue(1,0); pi.SetValue(2,1); pi.SetValue(3,2); pi.SetValue(4,3); matlab.PutFullMatrix("a", "base", pr, pi); System.Array prresult = new double[4]; System.Array piresult = new double[4]; matlab.GetFullMatrix("a", "base", ref prresult, ref piresult); } } }
Automation Server Functions | MATLAB Automation Properties |
© 1994-2005 The MathWorks, Inc.