Click here to Skip to main content
15,886,019 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 563.3K   26.1K   127  
A library to access MATLAB from .NET and a comparision of three possible methods to implement it.
// Matlab Interface Library
// by Emanuele Ruffaldi 2002
// http://www.sssup.it/~pit/
// mailto:pit@sssup.it
//
// Description: test of MAT File API
using System;
using EngMATLib;

namespace TestMATfiles
{
	/// <summary>
	/// Testing of the MAT library
	/// </summary>
	class Test
	{
		static void ListMATFile(string name)
		{
			using (MATFile mat = new MATFile(name, FileAccess.Read))
			  {
				if(!mat.IsOpened)
				{
					Console.WriteLine("Cannot open file {0}", mat.Filename);
					return;
				}

				NamedMatrixCollection mtx = mat.Variables;
				foreach(string s in mtx.Keys)
				{
					Console.WriteLine(mtx[s]);
				}		
			}
		}

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main(string[] args)
		{
			ListMATFile("demos.mat");
			double[,] myma = null;

			using (MATFile mat = new MATFile("demos.mat", FileAccess.Read))
			{
				
				if(mat.GetMatrix("long", ref myma))
				{
					Console.WriteLine("Got Matrix {0}", myma[0,0]);
				}
			}
			using (MATFile mat = new MATFile("demos2.mat", FileAccess.Write))
			{
				mat.PutMatrix("hello", myma);
			}
			ListMATFile("demos2.mat");
		}
	}
}

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