Lab#

Lab#

Description

Lab# is a .Net library written in C# 2.0 to interact with MATLAB® from any .Net application.
Technically Lab# is a wrapper around the MATLAB engine API. This API allow you to get and put matrices in MATLAB.

Download

Version 1.0 Alpha 1 source code

Latest version is always in SVN, the URL is : https://svn.sourceforge.net/svnroot/labsharp.
If you don't have any SVN client, under windows Tortoise SVN is a good choice (Integrated into the shell).

Exemples

// This exemple create an array with the values of the function
// f(x) = sin(x / 10); on the [0..100] range and display it in matlab.

using System;
using LabSharp;

class Program
{
    static void Main(string[] args)
    {
        double[] sin = new double[100];

        for (int i = 0; i < sin.Length; i++)
        {
            sin[i] = Math.Sin(i / 10.0);
        }
        using (Engine eng = Engine.Open())
        {
            eng.SetVariable("sin", sin);
            eng.Eval("plot(sin); figure(gcf)");
        }
    }
}
// This exemple fill an array with random values, send it to MATLAB and use
// MATLAB to calculate the mean value, then display the result in C#.

using System;
using LabSharp;

class Program
{
    static void Main(string[] args)
    {
        Random rnd = new Random();
        double[] dataArray = new double[100];
        for (int i = 0; i < dataArray.Length; i++)
        {
            dataArray[i] = rnd.NextDouble() * 100;
        }
        using (Engine eng = Engine.Open())
        {
            eng.SetVariable("data", dataArray);
            eng.Eval("data_mean = mean(data)");
            Console.WriteLine("The mean is : {0}", eng.GetVariable<double>("data_mean"));

            // Clean up
            eng.Eval("clear data");
            eng.Eval("clear data_mean");
        }
        Console.ReadKey();
    }
}

FAQ

What is the license of Lab# ?
Lab# uses the GNU Lesser General Public License (LGPL) license. Here is a simple description :
Why did this library exists, as MATLAB now provide .Net support ?
It's true that MATLAB starting from R2006a support direct access from .Net with the MWArray class. But this project was started before, and i still think that it have some advantages : There are also features not supported by Lab# but provided by MWArray like Cell Arrays, and an official support from MathWorks.