Click here to Skip to main content
15,896,439 members
Articles / Programming Languages / C#

1..2..3 ways of integrating MATLAB with the .NET

Rate me:
Please Sign up or sign in to vote.
4.82/5 (62 votes)
18 Nov 20037 min read 564.9K   26.1K   127  
A library to access MATLAB from .NET and a comparision of three possible methods to implement it.
using System;
using EngMATLib;
using System.Runtime.InteropServices;

namespace TestMATEng
{
	class MyDLL
	{
		/// <summary>
		/// 
		/// </summary>
		[DllImport("AAX.dll")]
		static extern IntPtr mlfAA(IntPtr a);
	
		/// <summary>
		/// External interface to my MATLAB function
		/// </summary>
		/// <param name="m"></param>
		/// <returns></returns>
		static public Matrix mlfAA(Matrix m)
		{
			return new Matrix(mlfAA((IntPtr)m));
		}
	}
	/// <summary>
	/// Summary description for Class1.
	/// </summary>
	class Test
	{


		[DllImport("AAX.dll")]
		public static extern void AAXInitialize();

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main(string[] args)
		{
			using (EngMATAccess mat = new EngMATAccess())
			{
						
				double [,] a = new double [20,20];
				a[0,0] = 10;
				a[0,1] = 20;
				a[1,0] = 30;
				int cols;

				// initializes the custom library
				AAXInitialize();

				Matrix rmax = MyDLL.mlfAA(new Matrix(a, false));
				Console.WriteLine("Result data is {0}x{1}", rmax.Rows, rmax.Cols);
				double [,] r = rmax.AsMatrix();
				Console.WriteLine("\tFirst two result values are {0} {1}", r[0,0],r[0,1]);

				EngMATAccess.MxArrayToMatrix(MyDLL.mlfAA(new Matrix(r)), ref r);
				Console.WriteLine("\tResults again is {0} {1}", r[0,0],r[0,1]);
				Console.WriteLine("Press any key to terminate");
				Console.ReadLine();
			}
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

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


Written By
Software Developer (Senior) Scuola Superiore S.Anna
Italy Italy
Assistant Professor in Applied Mechanics working in Virtual Reality, Robotics and having fun with Programming

Comments and Discussions