📜 ⬆️ ⬇️

Integrating MATLAB into C # .NET

Introduction


In this article I will try to tell you as fully and step by step how to connect MATLAB with C # .NET and make an application with an interface using the example of building a 3D plane.

Why do you need it?


Very often, the programmer is faced with the task of calculating complex mathematics. MATLAB, in turn, is an excellent tool for solving, but weak in creating a full-fledged user application (you can use MATLAB's GUI tools, but this did not suit me).

Instruments


  1. Microsoft Visual Studio 2008 SP1
  2. MATLAB 2010a
  3. MATLAB Component Runtime

')

Step 1. Setting up the linker


To build a MATLAB dll for integration into C # .NET, you need to configure a linker, i.e. what environment will we build the project. First you need to install the MCR runtime environment (MATLAB Component Runtime). This is a set of dll-libraries for full support of the MATLAB language. The installation file can be found: ... \ MATLAB \ R2011b \ toolbox \ compiler \ deploy \ win32 \ MCRInstaller. Installation is typical, click next.
To set up a linker in the MATLAB command window, type mbuild -setup . We agree with everything and choose the medium we need, in our case it is MVS 2008 SP1. We get something similar:

Please choose your compiler for building standalone MATLAB applications:

Would you like mbuild to locate installed compilers [y]/n? y

Select a compiler:
[1] Lcc-win32 C 2.4.1 in C:\PROGRA~1\MATLAB\R2010a\sys\lcc
[2] Microsoft Visual C++ 2008 SP1 in C:\Program Files\Microsoft Visual Studio 9.0

[0] None

Compiler: 2

Please verify your choices:

Compiler: Microsoft Visual C++ 2008 SP1
Location: C:\Program Files\Microsoft Visual Studio 9.0

Are these correct [y]/n? y


We get joyful Done . All - the linker is configured.

Step 2. Write the m-function


Let's write a function for constructing a 3D-plane in the given boundary conditions, for more interest, we will return the function descriptor.

function res=plane(strfunc,vx0,vx1,vy0,vy1,h)
vx=vx0:h:vx1;
vy=vy0:h:vy1;
figure(1)
res=ezsurfc(strfunc,vx,vy);
end


As you can see, the function consists of six lines of code, however, it performs all of the above actions: it accepts a character function ( strfunc ), initial and final values ​​of the plane boundary vectors (v x0,vx1,vy0,vy1 ), grid spacing ( h ) and returns the descriptor ( res ).
Save this code as plane.m.

Important: the MATLAB compiler understands only functions, i.e., each script must begin with function (preferably end ) and be a separate m-file.

Step 3. We receive dynamic library


We deploytool in the command window MATLAB'a deploytool . Create a new .NET Assembly project MATLABplane, specify the location.

image

Next, create the planeClass class, add plane.m to it, and press the build button

image

After successful compilation, the MATLABplane.dll library of interest is created; it will be located here: ... MATLABplane \ distrib \ MATLABplane.dll.

Step 4. Create a C # .NET application


In MVS 2008 SP1, we create a Windows Forms application in C #.

Add links to libraries

Before using the project methods, you need to add links to the compiled library MATLABplane.dll and the library MWArray.dll, you can find it at ... \ MATLAB \ R2010a \ toolbox \ dotnetbuilder \ bin \ win32 \ v2.0. After adding should be:

image

To use libraries in a project, you must add a namespace description:

using MathWorks.MATLAB.NET.Utility;
using MathWorks.MATLAB.NET.Arrays;
using MATLABplane;

Create a form

Using the toolbar in the constructor, we create the form for the future application.

image

The following blocks were used to create the form: Label, TextBox, RichBox, Button.

Write the code

For the interaction of the C # and MATLAB programming languages, the corresponding MATLAB data type MWArray C # has been created. MWArray is an array of arrays, it can consist of variables, scalars, vectors, matrices, strings, structures, objects, etc. To get any values ​​from MWArray, use type casting.
The algorithm of the application should be as follows:
  1. Getting the function in symbolic form and values ​​from text fields
  2. Calling the plane method from the planeClass class
  3. Getting the output descriptor array (type MWNumericArray)
  4. Output descriptor array to richbox

Below is the complete code with comments:

 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 MathWorks.MATLAB.NET.Utility; using MathWorks.MATLAB.NET.Arrays; using MATLABplane; namespace planeApp { public partial class Form1 : Form { public Form1() { InitializeComponent(); button1.Click += new EventHandler(button1_Click);//  button2.Click += new EventHandler(button2_Click); } double x0, x1, y0, y1, h; //  string func, s_x0, s_x1, s_y0, s_y1, s_h; MWArray[] res = null; //   plane MWNumericArray descriptor = null; //   private void button1_Click(object sender, EventArgs e)// { try { func = textBox1.Text; //  TextBox s_x0 = textBox2.Text; s_x1 = textBox3.Text; s_y0 = textBox4.Text; s_y1 = textBox5.Text; s_h = textBox6.Text; MWCharArray mw_func = new MWCharArray(func);//     MWCharArray x0 = Convert.ToDouble(s_x0); // string  double x1 = Convert.ToDouble(s_x1); y0 = Convert.ToDouble(s_y0); y1 = Convert.ToDouble(s_y1); h = Convert.ToDouble(s_h); planeClass obj_plane = new planeClass(); //   res = obj_plane.plane(1, mw_func, x0, x1, y0, y1, h);//   plane,   -  -   descriptor = (MWNumericArray)res[0]; //     MWArray      MWNumericArray double[,] d_descriptor = (double[,])descriptor.ToArray(MWArrayComponent.Real);//  MWNUmericArray    double for (int i = 0; i < d_descriptor.Length; i++)//  d_descriptor  RichBox { richTextBox1.Text += i.ToString() + '\t'; richTextBox1.Text += d_descriptor[i, 0].ToString("0.000") + '\n';//   double  string } } catch (Exception ex)//  { System.Windows.Forms.MessageBox.Show(ex.Message); } } private void button2_Click(object sender, EventArgs e) { richTextBox1.Text = string.Empty;// RichBox res = null;//  descriptor = null; } } } 


As you can see, type casting is actively used, according to the MWArray-> MWNUmericArray-> C # type scheme.
Compile the project and see the result:

image

Literature


  1. MATLAB. Programming in Visual C #, Borland JBuilder, VBA - N. K. Smolentsev
  2. C # and the .NET platform - E. Troelsen

Source: https://habr.com/ru/post/132487/


All Articles