Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Write line to MATLAB from C#

Tags:

c#

matlab

I want to write a line in the MATLAB command window from a C# method. This is the .NET code:

using System;

namespace SharpLab {
    public class Test {
        public void Run() {
            dynamic Matlab = Activator.CreateInstance(Type.GetTypeFromProgID("Matlab.Application"));
            Matlab.Execute("clc"); // This line does work.
            Matlab.Execute("disp('Hello world!')"); // This line does not work.
        }
    }
}

Now I load the library, create a class instance and run the method. This is the MATLAB code:

disp('This message goes to the command window. Can .NET call clc?');
NET.addAssembly('SharpLab.dll');
Test = SharpLab.Test;
Test.Run();

This does run and the command window is cleared by clc. The second call, 'Hello world!', does not work.

How can I print messages from C# in the MATLAB command window?

EDIT: I received a message linking to http://www.mathworks.nl/support/solutions/en/data/1-C9Y0IJ/index.html?product=SL&solut=. This solution collects all the written information into a variable to use, however the real function I am running does work for about a minute with a lot of messages in between. Waiting a minute before throwing a wall of text is not what I am after.

like image 954
Deathspike Avatar asked Oct 03 '22 18:10

Deathspike


1 Answers

How about using .NET events to inform listeners that an event occurred, where you register an event handler in MATLAB to do the actual printing.

Here is a toy example that finds all prime numbers up to 10000. First we create the C# library:

MyClass.cs

using System;

namespace MyLibrary
{
    public class MyClass
    {
        // function that does some work and notify listeners of occurred events
        public void FindPrimes()
        {
            // Primes between 1 and 10000
            for (int i = 1; i < 10000; i++)
            {
                if (MyClass.isPrime(i))
                {
                    //System.Console.WriteLine(i);
                    onPrimeFound(i);
                }
            }
        }

        // helper function to determine if number is prime
        public static bool isPrime(int x)
        {
            if (x == 1) return false;
            if (x == 2) return true;
            for (int i = 2; i <= Math.Ceiling(Math.Sqrt(x)); i++)
            {
                if (x % i == 0) return false;
            }
            return true;
        }

        // event broadcasted
        public event EventHandler PrimeFound;
        protected void onPrimeFound(int x)
        {
            var handler = this.PrimeFound;
            if (handler != null)
            {
                handler(this, new PrimeEventArgs(x));
            }
        }
    }

    // event data passed to listeners
    public class PrimeEventArgs : EventArgs
    {
        public readonly int number;
        public PrimeEventArgs(int x)
        {
            this.number = x;
        }
    }
}

MATLAB

Next we use our class library inside MATLAB:

>> NET.addAssembly('C:\path\to\MyLibrary.dll');
>> c = MyLibrary.MyClass();
>> lh = addlistener(c, 'PrimeFound', @(o,e) fprintf('Prime: %d\n', e.number));
>> c.FindPrimes()
Prime: 2
Prime: 3
Prime: 5
...
Prime: 9973

The C# function FindPrimes() performs a lengthy operation, while emitting events to let interested observers know of occurred events (basically whenever you want to print something to MATLAB console). It should print immediately without buffering.

like image 91
Amro Avatar answered Oct 05 '22 23:10

Amro