C # mixed programming with MATLAB,

Source: Internet
Author: User

C # mixed programming with MATLAB,

 

The hybrid programming of C # and MatLab makes full use of the visual display of winform and the powerful computing capability of matlab. Here is a small example to illustrate the implementation of mixed programming between the two.

  

I. Software configuration instructions

C # version: VS2010 matlab version: maid

 

2. Compile the matlab function into a dll file

 

2.1 first write a function's. m file (the following function MatrixOpera implements the addition and subtraction of two matrices)

% ---------------------------------------------------- Function [addRlt, minusRlt] = MatrixOpera (a, B) % matrix addition, subtract [m1, n1] = size (a); [m2, n2] = size (B); if m1 ~ = M2 | n1 ~ = N2 display ('inconsistent matrix sizes ') error ('parameter error'); endaddRlt = a + B; minusRlt = a-B; end % -----------------------------------------------------------

2.2 create a dll file project

  In the MatLab command window, enter deploytool to open the Project Creation window for creating dll files, as shown in

MatPrj. dll and D: \ Program Files \ MATLAB \ R2013a \ toolbox \ dotnetbuilder \ bin \ win64 \ v2.0 (the front side is the installation path of matlab) under MWArray. copy the dll to the \ bin \ Debug folder under the VS Project (callmatlab dllapp ).

  

  3.3 Add a dll file reference in VS and then reference the corresponding namespace before the code

  

  

 

    

  

If the matlab function is complex and requires additional space, you can add it based on your experience.

So far, we can use c # To Call The functions we have compiled using matlab.

 

4. Call the functions provided by matlab in C #

  A matrix of m * n in matlab. net is a 1 * (m * n) array. Therefore, the key to calling the matlab function in C # is to understand the conversion between a matrix with the same data type and an array. There are two problems:

A. When you input parameters in C # To the matlab function, you need to convert the parameters to the parameter form of matlab, which is usually of the MWArray type.

B. The parameters returned by the matlab function must also be converted to the C # type, such as the array or numeric type.

 

The code for calling the function in C # is as follows:

MatPrj. myMathClass test = new matPrj. myMathClass (); // instantiate the myMathClass class double [] a = {1, 2, 3, 4, 5, 6 }; // define two input parameters double [] B = {1, 1, 1, 1, 1}; // they are two one-dimensional static arrays double [,] c = new double [3, 2]; // defines the type of the received output parameter in C #. double [,] d = new double [3, 2]; // two-dimensional arrays // convert both input parameters to the intermediate type, the intermediate type is also a matrix, so you need to specify the dimension. // here, the two input parameters are converted to the MWNumericArray (3, 2, a) matrix with two three rows and two columns ); MWNumericArray maid = new MWNumericArray (3, 2, B); // The input parameter is successfully converted to two MWArray element types: MWArray [] agrsIn = new MWArray [] {mat_a, mat_ B }; // declare that the output parameter is of two MWArray element types. Be sure to write the number of MWArray [] agrsOut = new MWArray [2]; // call the matlab function. 2 indicates the number of output parameters, add the ref keyword before the output parameter. // In this example, the matrix of two three rows and two columns is added and subtracted. matrixOpera (2, ref agrsOut, agrsIn); // convert two output parameters to the intermediate type MWNumericArray net_c = agrsOut [0] as MWNumericArray; // The first output parameter of the matlab function: MWNumericArray net_d = agrsOut [1] as MWNumericArray; // The second output parameter // converts it to the receiving parameter C = (double [,]) net_c.ToArray () in c (); // convert to a two-dimensional array d = (double [,]) net_d.ToArray (); // be sure to convert the received parameters, different types of receiving parameters use different conversion functions/two-dimensional arrays use the ToArray () function to convert/one-dimensional arrays use ToVector (MWArrayComponent. real) Function Conversion // ToScalarDouble () function conversion for a single double value // ToScalarInteger () function conversion for (int I = 0; I <= 2; I ++) // verify the output result {for (int j = 0; j <= 1; j ++) {textBox1.Text + = c [I, j]. toString () + ""; textBox2.Text + = d [I, j]. toString () + "";} textBox1.Text + = "\ r \ n"; // wrap textBox2.Text + = "\ r \ n ";}

 

MWNumericArray is an intermediate class for Data Conversion Between MWArray and C #. It implements the function of passing parameters between C # and matlab functions.

MWArray is the total type. Other variable types starting with MW and ending with Array can be directly assigned values or values. the standard internal type of the m file after compilation. All C # types must be converted to this type before the matlab function can be called as a parameter.

  

When I run the above program, the first sentence returns an error when instantiating the myMathClass class.

 

However, there is no problem with this statement.

MatPrj. myMathClass test = new matPrj. myMathClass ();

I checked the error information, but the solution was basically solved by changing the matlab version. I accidentally saw that the HRESULT: 0x8007000B error has some relationship with the compilation platform, I started to try to see if it was caused by a problem with my platform. It was enough to change the compiling platform.

Specific solution:

1: Right-click the project name and choose Properties> Generate

2: Select x64 for the target platform (because my matlab is 64-bit)

 

Run the program again and the result is as follows:

 

The above results are the same as those in matlab to verify the correctness of the results.

 

How to run programs freely without the installation of matlab:

Hybrid programming is implemented through com. If MATLAB is not installed on the machine, the compilation software can still be used, but the MATLAB Compiler Runtime (MCR) provided by MATLAB must be installed ), in the installation directory of MATLAB, the software is: X: \ MATLAB \ R2013a \ toolbox \ compiler \ deploy \ win32 \ MCRInstaller.exe (X is the drive letter installed by MATLAB ), if MATLAB is installed on your computer, you do not need to reinstall the software. In addition, the MATLAB Compiler Runtime provided by different MATLAB versions may be different. If there is a problem with running the software compiled by yourself on another computer, it may be caused by different Compiler versions.

 

 

Reference: http://wenku.baidu.com/link? Url = mgBBgQFt89s-7rKrNbJ-630AH0q_2mMMYBbvCWx9IiGkuOQ7qFTkL_pZRhT5FqazmtFoD6UUM6vWLeDrprF2OAGqUcmrHEugVntjWvzCwxy

Http://www.cnblogs.com/liuxiaowei0543/p/3682377.html

Http://wenku.baidu.com/view/6ad3b62b59eef8c75fbfb3ed.html

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.