Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Register.NET C# Classes in Matlab

0.00/5 (No votes)
13 Mar 2009 1  
One way to use .NET Classes from matlab

Introduction

I encountered this problem when my boss came to me with the idea of creating a new hardware driver for testing our new product. Well, he also wanted that it will be implemented with .NET C# environment. After we came up with the perfect design (that match all the users requirements and our needs) we started implementing the driver engine - only to find out that a new requirement was raised ! The driver had to support Matlab users as well... the rest is history...

In this article, I will try to explain how one can use .NET class from Matlab, in a simple and helpful way. So let me explain it like a "recipe".

1. Code Settings - Define Classes as COM Objects

First, add System.Runtime.IntropServices library to your class component.

Second, add a class interface to all sub-classes in the class project and declare them as a visible Com.

using System.Runtime.InteropServices;
...
namespace EshelProject
{
    [ClassInterface(ClassInterfaceType.AutoDual)]
    [ComVisible(true)]
     public class HW_Parallel : RFComm [...]
}

2. Project Settings

Go to assembly information:

pic1.jpg

Change assembly version to a fixed version - you can modify it manually later. Make assembly Com-Visible:

pic2.jpg

Select Signing and Sign the assembly. Create a strong name key:

pic3.jpg

Uncheck protect my key with a password box and enter a valid key file name - My recommendation is that you use the same name as class name.

4. Rebuild the Solution

Repeat section 1 - 4 for every sub-class / classes in your project.

5. .NET Visual Studio Shell

Register the project into O.S registry by opening Visual Studio command prompt and type:

Regasm <dllName.dll> /tlb

If you want your DLLs to be "pathless" from Matlab environment, then type:

gacutil /i <dllname.dll> 

Use the Class in Matlab

Simply type from Matlab console:

<item name> Actxserver('<namespace>.<class name>')
pic4_small.JPG

To see all your class functions, type:

invoke(<item name>);

And to actually use them, type:

<item name>.<method name>(input values);

Registry Verification and Notes

Go to [start] -> [run] and type regedit. Look for your <namespace>.<class name> under the section HKEY_CLASSES_ROOT. If your project has references to other projects' namespaces, don’t forget to repeat this session for the projects it references.

If you want to debug the class as it runs from mat-lab, open .NET project in Visual Studio and go to:

Debug>Attach to process…>MatLab.exe

pic5.jpg

History

  • 13th March, 2009: Initial post

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here