Click here to Skip to main content
15,895,799 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.8K   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: GUI test of MATLAB DDE interface
// This Application shows how to interact directly with MATLAB
// using DDE (see the corresponding VB 6.0 example)
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using DDELibrary;

namespace DDEMatSpy
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		private System.Windows.Forms.TextBox textIn;
		private System.Windows.Forms.TextBox textOut;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Label label2;
		private DDEClient dde;
		private DDEChannel channel;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public Form1()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.textIn = new System.Windows.Forms.TextBox();
			this.textOut = new System.Windows.Forms.TextBox();
			this.label1 = new System.Windows.Forms.Label();
			this.label2 = new System.Windows.Forms.Label();
			this.SuspendLayout();
			// 
			// textIn
			// 
			this.textIn.Anchor = ((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right);
			this.textIn.Location = new System.Drawing.Point(0, 24);
			this.textIn.Name = "textIn";
			this.textIn.Size = new System.Drawing.Size(616, 20);
			this.textIn.TabIndex = 0;
			this.textIn.Text = "A = [ 1 2 3]";
			this.textIn.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.textIn_KeyPress);
			this.textIn.TextChanged += new System.EventHandler(this.textIn_TextChanged);
			// 
			// textOut
			// 
			this.textOut.Anchor = (((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
				| System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right);
			this.textOut.Location = new System.Drawing.Point(0, 72);
			this.textOut.Multiline = true;
			this.textOut.Name = "textOut";
			this.textOut.ReadOnly = true;
			this.textOut.Size = new System.Drawing.Size(616, 192);
			this.textOut.TabIndex = 1;
			this.textOut.Text = "";
			// 
			// label1
			// 
			this.label1.Name = "label1";
			this.label1.TabIndex = 2;
			this.label1.Text = "Input";
			this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(0, 48);
			this.label2.Name = "label2";
			this.label2.TabIndex = 3;
			this.label2.Text = "Output";
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(624, 273);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.label2,
																		  this.label1,
																		  this.textOut,
																		  this.textIn});
			this.Name = "Form1";
			this.Text = "MATLAB DDE Test";
			this.Load += new System.EventHandler(this.Form1_Load);
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new Form1());
		}

		private void Form1_Load(object sender, System.EventArgs e)
		{
			dde = new DDEClient();
			channel = dde.OpenChannel("MATLAB", "Engine");
			if(channel == null)
			{
				MessageBox.Show("Cannot connect with MATLAB, please open it and retry");
				Application.Exit();
			}
		}

		private void textIn_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
		{
			if(e.KeyChar == 13)
			{
				string command = textIn.Text;
				channel.Execute(command, "EngEvalString");					
				if(channel.Request("EngStringResult", out command)) 
				{
					textOut.Text = command;
				}
				else
					textOut.Text = "";
			}
		}

		private void textIn_TextChanged(object sender, System.EventArgs e)
		{
		
		}
	}
}

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