Click here to Skip to main content
Click here to Skip to main content

Tagged as

Register.NET C# Classes in Matlab

, 13 Mar 2009 CPOL
Rate this:
Please Sign up or sign in to vote.
One way to use .NET Classes from matlab


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
     public class HW_Parallel : RFComm [...]

2. Project Settings

Go to assembly information:


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


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


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>')

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



  • 13th March, 2009: Initial post


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Israel Israel
No Biography provided

Comments and Discussions

GeneralAlways nice to find examples from old good friends :) PinmemberFire Cracker4-Jun-14 22:42 
Generaldown the israil Pinmemberreza_ali2020009-Jun-11 5:42 
GeneralI am getting following error ... Pinmemberraja24134-Feb-10 1:05 
I did as u explained...
One thing i missed out is i could not register that in to registry as i dont have admin permissions. i tried to run the command
it is giving me following error
Error using ==>; actxserver
Server creation failed. Invalid ProgID 'NameSpaceToBeUsedByMATLAB.ClassToBeUsedByMATLAB'

what could be the reason...
And one more doubt....
My dll name is Test.dll
one one name space is there .. that is 'NameSpaceToBeUsedByMATLAB
only one class in that name space is present. That is ClassToBeUsedByMATLAB'

Actxserver('NameSpaceToBeUsedByMATLAB.ClassToBeUsedByMATLAB') this stmt is making use of name space and class. But first dll should be loaded, right?. or some how it should get refernce of that Test.dll
How does it come to know 'NameSpaceToBeUsedByMATLAB.ClassToBeUsedByMATLAB' belongs to Test.dll only.
pleasee clarify me.. I dont know anything about MATLAB. I am working on a requirement that .net dll should be consumed by MATLAB.
i am a .net programmer so pls provide input for this.
GeneralRe: I am getting following error ... Pinmembermorisson_c4-Feb-10 2:27 
GeneralRe: I am getting following error ... Pinmemberraja24134-Feb-10 21:55 
GeneralRe: I am getting following error ... Pinmembermorisson_c6-Feb-10 21:41 
GeneralRe: I am getting following error ... Pinmemberraja24137-Feb-10 18:31 
GeneralMATLAB-r2008+C#-2005 Pinmemberwashi_w_z_r29-Jul-09 8:19 
GeneralRe: MATLAB-r2008+C#-2005 [modified] Pinmembermorisson_c9-Aug-09 5:02 
GeneralClear an simple - Thank you! Pinmemberramiyam16-Mar-09 23:02 
GeneralRe: Clear an simple - Thank you! Pinmembermorisson_c16-Mar-09 23:28 
GeneralMATLAB's R2009a now supports calling .Net Assemblies PinmemberStephenrlrl13-Mar-09 7:48 
GeneralRe: MATLAB's R2009a now supports calling .Net Assemblies [modified] Pinmembermorisson_c14-Mar-09 3:04 
GeneralRe: MATLAB's R2009a now supports calling .Net Assemblies PinmemberA. Riazi17-Mar-09 7:01 
GeneralRe: MATLAB's R2009a now supports calling .Net Assemblies Pinmemberandre1234513-Jul-09 10:00 
GeneralRe: MATLAB's R2009a now supports calling .Net Assemblies Pinmembermorisson_c24-Aug-09 12:08 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150302.1 | Last Updated 13 Mar 2009
Article Copyright 2009 by morisson_c
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid