Skip to Main Content Skip to Search
Login
File Exchange
MATLAB Newsgroup
Link Exchange
  Blogs  
 Contest 
MathWorks.com

Thread Subject: Interfacing Matlab with C#

Subject: Interfacing Matlab with C#

From: Daniel

Date: 16 Jan, 2008 17:24:02

Message: 1 of 6

I have a program that will be passing data to a C# script.
C# will then be modifying the data and I need it to be sent
to Matlab. Matlab will perform some function and then pass
the data back to C#. This all needs to be done in real
time, and not necessarily on the same computer. So how do
I pass data from C# to Matlab? I've been reading a lot of
help files in the Simulink and Real Time Workshop
categories, but I can't piece it all together to get a
useful answer. Most of my searches online have led to
people wanting to call C# from Matlab or convert their
Matlab files to C# or things like that. I want to have a
stand-alone Matlab file (probably a Simulink model) that
just sits there waiting for information, and have the
information coming from a separate C# program. Is this
doable and how?

Subject: Re: Interfacing Matlab with C#

From: J Shaffer

Date: 16 Jan, 2008 20:00:20

Message: 2 of 6

I've done this by writing an IDispatch COM interface around
the C# method and then invoking it in MATLAB through DCOM
with activexserver.


Subject: Re: Interfacing Matlab with C#

From: Jaco de Groot

Date: 06 Feb, 2008 14:51:14

Message: 3 of 6

"Daniel " <cowmaster6@yahoo.com> wrote in message
<fmleji$33i$1@fred.mathworks.com>...
> I have a program that will be passing data to a C#
script.
> C# will then be modifying the data and I need it to be
sent
> to Matlab. Matlab will perform some function and then
pass
> the data back to C#. This all needs to be done in real
> time, and not necessarily on the same computer. So how
do
> I pass data from C# to Matlab? I've been reading a lot
of
> help files in the Simulink and Real Time Workshop
> categories, but I can't piece it all together to get a
> useful answer. Most of my searches online have led to
> people wanting to call C# from Matlab or convert their
> Matlab files to C# or things like that. I want to have a
> stand-alone Matlab file (probably a Simulink model) that
> just sits there waiting for information, and have the
> information coming from a separate C# program. Is this
> doable and how?

Register your C# as COM library. Right click on your
project, go to Build and check the box "Register as Com
Interop". Then create an interface for the methods you want
to expose to COM and add the following attributes. A GUID
can be created in VS2005 (see Tools)

[Guid("xxxxxxxx-xxxx-xxxx-xxx-xxxxxxxxxxxx")]
[ComVisible(true)]
public interface IComInterface
{
      char SeparationCharacter { get;}

      bool LogOff(string userID);
}

Implement the interface on a class and set the following
attributes on this class

[ProgId("MyCom.1.0.0")]
[ClassInterface(ClassInterfaceType.None)]
[Guid("xxxxxxxx-xxxx-xxxx-xxx-xxxxxxxxxxxx")]
[ComVisible(true)]
public class ComClass: IComInterface
{
     public char SeparationCharacter
        {
            get
            {
                return '#';
            }
        }

        public bool LogOff(string userID);
        {
            return true;
        }

}

Create an instance of this class using actxserver:

>h = actxserver('MyCom.1.0.0')]

h.get will receive all setter properties:

->SeparationCharacter: 35

separationCharacter = h.SeparationCharacter

Invoke will return all methods

>h.invoke

->LogOff = bool LogOff(handle, string)

The method can be invoked like this:

>success = h.LogOff('me')

->success = 1

I hope this is useful

Jaco de Groot

Subject: Re: Interfacing Matlab with C#

From: Dimitri Shvorob

Date: 21 Jun, 2008 17:06:02

Message: 4 of 6

.. Many questions spring to mind, but one is 'How would
Matlab know of the new DLL?'

Subject: Re: Interfacing Matlab with C#

From: Wagner Lima

Date: 25 Jun, 2008 19:17:01

Message: 5 of 6

This article is very helpful.
http://www.codeproject.com/KB/dotnet/matlabeng.aspx

I've developed applications using solution #1 and it works
perfectly.

Subject: Re: Interfacing Matlab with C#

From: Dimitri Shvorob

Date: 25 Jun, 2008 19:46:01

Message: 6 of 6

Thank you, Wagner, but the challenge is to call C# from
Matlab, not Matlab from C#.

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
interface Daniel 16 Jan, 2008 12:25:00
c Daniel 16 Jan, 2008 12:25:00
simulink Daniel 16 Jan, 2008 12:25:00
real time Daniel 16 Jan, 2008 12:25:00
rssFeed for this Thread

envelope graphic E-mail this page to a colleague

Public Submission Policy
NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Disclaimer prior to use.
Related Topics