# [Matlab] Execute Matlab function from C#

This code makes you execute a matlab function from C#. Essentially, C# runs a Matlab instance, so you can run any command or function from C# in Matlab. I needed this to make a DSS instance in Microsoft Robot Studio (which i havent finished yet). It’s pretty neat, you could use it when you have a smart algorithm in Matlab, you can just run the code from C# and get the result.

The code is below, or you just download the zipfile which contains everything you need.

Csharp_Matlab

## Main Program

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

using System.Diagnostics;

using MLApp;

`namespace WindowsFormsApplication1`

`{`

public partial class Form1 : Form

`{`

private MLApp.MLApp matlab;

private Stopwatch stopwatch;

public Form1()

`{`

InitializeComponent();

`}`

public String matlab_Command(String cmd)

`{`

stopwatch.Start();

String ans = matlab.Execute(cmd);

stopwatch.Stop();

matlabLog.AppendText(">> " + cmd + " [ " + stopwatch.ElapsedMilliseconds.ToString() + " ]n");

stopwatch.Reset();

matlabLog.AppendText(ans);

return ans;

`}`

private void Form1_Load(object sender, EventArgs e)

`{`

log.AppendText("Creating MATLAB objectn");

matlab = new MLApp.MLApp();

log.AppendText("MATLAB object createdn");

log.AppendText("Creating Stopwatch objectn");

stopwatch = new Stopwatch();

log.AppendText("Stopwatch object createdn");

`//!!!!!!!!! INPUT YOUR OWN WORKING FOLDER AND MATLABDIR HERE`

log.AppendText("Changing MATLAB working dir to C:UsersTimDesktopRobot StudioMatlabn");

matlab_Command("cd C:UsersTimDesktopRobot StudioMatlab");

matlab_Command("pwd");

`}`

private void button1_Click(object sender, EventArgs e)

`{`

`//PUT HERE WHAT YOU WANT TO EXECUTE IN MATLAB`

matlab_Command("test");

`//double a = matlab.GetVariable("ans", "base");`

`//log.AppendText(a.ToString());`

`}`

private void autoScroll_TextChanged(object sender, EventArgs e)

`{`

((RichTextBox)sender).ScrollToCaret();

`}`

`}`

`}`

## Main Form

using System;

using System.Collections.Generic;

using System.Linq;

using System.Windows.Forms;

`namespace WindowsFormsApplication1`

`{`

static class Program

`{`

`/// <summary>`

`/// The main entry point for the application.`

`/// </summary>`

[STAThread]

static void Main()

`{`

Application.EnableVisualStyles();

Application.SetCompatibleTextRenderingDefault(false);

Application.Run(new Form1());

`}`

`}`

`}`

Thank you so much.

I didn’t get the program working. It said (MLApp) not found. The next question, can I use this way with console or even with web application.

Best wishes

An important post. Thank you.

Thank you so much for kinect program with matlab

Hi

I have prepared a function in matlab and deployed it to .net assemply. but I can not call it in C#.net work space. Would you please help me in writing the correct code to call matlab function into C#?

Matlab Code is as follows:

(function C=Func1(A,B);

C=A+B;

end)

My program does not know “using MLApp;” Also I could not any reference to Matlab in project/add reference/ com. Could you tell me how I can add a instance of MLApp

Thank you so much…. am searching for this kind of function only….. it really works and helped me a lot………..

Thank you!!! This really works! I tried to use .DLL ( created by Matlab) approach, to call my matlab functions, but I couldn’t manage to pass images from C# to Matlab…

Top