[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

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.Diagnostics;
  10.  
  11. using MLApp;
  12.  
  13. namespace WindowsFormsApplication1
  14. {
  15.     public partial class Form1 : Form
  16.     {
  17.         private MLApp.MLApp matlab;
  18.         private Stopwatch stopwatch;
  19.  
  20.         public Form1()
  21.         {
  22.             InitializeComponent();
  23.         }
  24.  
  25.         public String matlab_Command(String cmd)
  26.         {
  27.             stopwatch.Start();
  28.             String ans = matlab.Execute(cmd);
  29.             stopwatch.Stop();
  30.             matlabLog.AppendText(">> " + cmd + " [ " + stopwatch.ElapsedMilliseconds.ToString() + " ]n");
  31.             stopwatch.Reset();
  32.             matlabLog.AppendText(ans);
  33.             return ans;
  34.         }
  35.  
  36.         private void Form1_Load(object sender, EventArgs e)
  37.         {
  38.             log.AppendText("Creating MATLAB objectn");
  39.             matlab = new MLApp.MLApp();
  40.             log.AppendText("MATLAB object createdn");
  41.             log.AppendText("Creating Stopwatch objectn");
  42.             stopwatch = new Stopwatch();
  43.             log.AppendText("Stopwatch object createdn");
  44. 			//!!!!!!!!! INPUT YOUR OWN WORKING FOLDER AND MATLABDIR HERE
  45.             log.AppendText("Changing MATLAB working dir to C:UsersTimDesktopRobot StudioMatlabn");
  46.             matlab_Command("cd C:UsersTimDesktopRobot StudioMatlab");
  47.             matlab_Command("pwd");
  48.         }
  49.  
  50.         private void button1_Click(object sender, EventArgs e)
  51.         {
  52. 			//PUT HERE WHAT YOU WANT TO EXECUTE IN MATLAB
  53.             matlab_Command("test");
  54.             //double a = matlab.GetVariable("ans", "base");
  55.             //log.AppendText(a.ToString());
  56.         }
  57.  
  58.         private void autoScroll_TextChanged(object sender, EventArgs e)
  59.         {
  60.             ((RichTextBox)sender).ScrollToCaret();
  61.         }
  62.     }
  63. }

Main Form

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Windows.Forms;
  5.  
  6. namespace WindowsFormsApplication1
  7. {
  8.     static class Program
  9.     {
  10.         /// <summary>
  11.         /// The main entry point for the application.
  12.         /// </summary>
  13.         [STAThread]
  14.         static void Main()
  15.         {
  16.             Application.EnableVisualStyles();
  17.             Application.SetCompatibleTextRenderingDefault(false);
  18.             Application.Run(new Form1());
  19.         }
  20.     }
  21. }

Tim Zaman

MSc Biorobotics. Specialization in computer vision and deep learning. Works at NVIDIA.

You may also like...

9 Responses

  1. Urusan says:

    Thank you so much.

  2. khaldon says:

    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

  3. Hakan_Tek says:

    An important post. Thank you.

  4. Thank you so much for kinect program with matlab

  5. Hadi says:

    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)

  6. reza says:

    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

  7. venkat says:

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

  8. nejc says:

    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…

  9. Phil says:

    Top