MATLAB: How to create and access a C# COM object from MATLAB

MATLAB

I want to create a COM object using C# and would like to call this COM object from MATLAB.

Best Answer

  • You can use MATLAB as a COM client to call COM objects using the ACTXSERVER or ACTXCONTROL functions. These COM objects do not need to be created in any special manner. For details about how to call a COM object from MATLAB, see the following chapter of the MATLAB External Interfaces manual.
    <http://www.mathworks.com/help/techdoc/matlab_external/brd4at8.html>
    The following is an example of how to create a simple COM object in C# and then call it from MATLAB. This example was created using Microsoft Visual Studio 2005. The exact steps may differ for other versions of Microsoft Visual Studio.
    1. Open Microsoft Visual Studio 2005 and select File->New Project. Then select Visual C#->Class Library and enter the name myCOMObject. Click OK.
    2. Replace the auto-generated code in Class1.cs with the following:
    using System;
    using System.Runtime.InteropServices;
    using System.Windows.Forms;
    using System.Text;
    using System.Diagnostics;
    namespace myCOMObject
    {
    //COM interface
    [Guid("9E015072-B61B-4aa2-9F49-19AC57F04B13"),
    InterfaceType(ComInterfaceType.InterfaceIsDual)]
    public interface ITestInterface
    {
    [DispId(1)] //
    void test();
    [DispId(2)] //
    string Mess(string s);
    [DispId(3)] //
    double[] ArrayTest(int n);
    }
    // COM events for actxserver command
    [ProgId("Test.Library"),
    Guid("3B177057-AA07-4017-8D52-137C1DA07ABA"),
    ClassInterface(ClassInterfaceType.AutoDual)]
    public class TestInterface : ITestInterface
    {
    public TestInterface()
    {
    //
    }
    public void test() //
    {
    Process p = null;
    p = new Process();
    p.StartInfo.FileName = "notepad";
    p.Start();
    p.WaitForExit();
    MessageBox.Show("Here is notepad");
    }
    public string Mess(string s) //
    {
    MessageBox.Show(s);
    return "Hello Jody";
    }
    public double[] ArrayTest(int n) //
    {
    return (new double[2] { 1, 5 });
    }
    }
    }
    3. Select Tools->Create GUID, click the Registry Format option and click the Copy button. Then paste this new GUID into the line
    //COM interface
    [Guid("9E015072-B61B-4aa2-9F49-19AC57F04B13"),
    Be sure to remove the curly braces {} from the pasted string.
    4. Next, click the New GUID button and copy-paste a second GUID into the line
    [ProgId("Test.Library"),
    Guid("3B177057-AA07-4017-8D52-137C1DA07ABA"),
    5. Select Project->Add Reference. In the .NET tab select System.Windows.Form and click OK.
    6. Open the AssemblyInfo.cs file and modify the following line from
    [assembly: ComVisible(false)]
    to
    [assembly: ComVisible(true)
    7. Right-click on the myCOMObject project and select Properties. Select the Build category and check the box for "Register for COM interop".
    8. Select Build->Build Solution. This will build the application and auto-register the application with the ProgID Test.Library.
    9. Now you can access the COM object from MATLAB. The following code creates a new instance of the COM object, displays the methods of that object and calls the test and Mess functions.
    h=actxserver('Test.Library')
    methodsview(h)
    h.test
    h.Mess('Hello')
    Note: MathWorks does not provide technical support for using Microsoft Visual Studio or offer guidance in the creation of COM objects outside of MATLAB.