The mixed programming of C # and MATLAB leverages the intuitive display of WinForm and the powerful computing power of MATLAB. Here is a small example of the form to tell you the implementation of the two hybrid programming.
First, the software configuration instructions
C # version: VS2010 matlab version: matlab2013a
Second, the MATLAB function compiled into a DLL file
2.1 First write a function of the. m file (The following function Matrixopera implements the addition and subtraction of two matrices)
%--------------------------------------------------------function [ADDRLT,MINUSRLT]=Matrixopera (A, b) the% matrix is added and subtracted [m1,n1]=size (a); [ M2,N2]=size (b); if m1~=m2 | | n1~=n2 display (' matrix size is not the same ') error (' Parameter error '); endaddrlt=a+b;minusrlt=a-b;end%---------------------- -------------------------------------
2.2 Creating a project for making DLL files
In the command window of MATLAB, enter deploytool to open the Project creation window that creates the DLL file, as shown in
In the diagram, there are three main parts, name: You want to kill the project name, PRJ is the suffix of the project; Location: Create the project's storage position; Type: For C #, we need to choose the. NET assembly
Click the OK option and the project has been created
2.3 Add the corresponding elements to the project and compile
In the above process, a new project called MATPRJ (which is equivalent to the namespace namespace in C #) is added, then the class is added to it (it is the classes in C #), the [Add class] adds the Mymathclass, and then the method is added to it (it is the function). Click [Add Files] to select the previously written Matrixopera function in this example. After that, it can be compiled, and it takes about two minutes to compile, as shown in
Iii. DLL references for adding MATLAB to C #
3.1 Open vs2011, create a new form application
3.2 Completing a copy of the DLL file
The MATLAB project (MATPRJ) src folder under the matPrj.dll and D:\Program files\matlab\r2013a\toolbox\dotnetbuilder\bin\win64\ v2.0 (the installation path to the MATLAB front) folder MWArray.dll Copy to the \bin\debug folder under vs Project (Callmatlabdllapp)
3.3 Add a reference to the DLL file in VS and then reference the appropriate namespace before the code
If the MATLAB function is complex, you need to use other space, depending on the situation, you can add your own experience.
At this point, we can already use C # to invoke the functions we have written in MATLAB.
Iv. invoking the functions provided by MATLAB in C #
A m*n matrix in MATLAB corresponds to an array of 1* (m*n) in. NET, so the key to invoking a Matlab function in C # is to master the conversion between a matrix and an array with a consistent data type. The following two questions are included:
A, when you enter the parameters in C # into the MATLAB function, you want to convert the parameters to matlab parameter form, usually the Mwarray type
b, the parameters returned by the MATLAB function are also converted to C # types, such as arrays or numeric types
The code for calling this example function in C # is as follows:
Matprj.mymathclass test =NewMatprj.mymathclass ();//instantiate the Mymathclass class Double[] A = {1,2,3,4,5,6};//define values for two input parameters Double[] B = {1,1,1,1,1,1};//They are two one-dimensional arrays Double[,] C =New Double[3,2];//defines the type of receive output parameters in C # Double[,] D =New Double[3,2];//are two-dimensional arrays//the two input parameters are converted to intermediate types, and the intermediate type is also the matrix, so to indicate the number of dimensions//Here we convert two input parameters to two three rows and two columns of matrixMwnumericarray matlab_a =NewMwnumericarray (3,2, a); Mwnumericarray Matlab_b=NewMwnumericarray (3,2, B); //input parameters successfully converted to a mwarray matrixmwarray[] Agrsin =Newmwarray[] {matlab_a, matlab_b}; //declaration output parameter is a mwarray matrix two output parameter, must write quantitymwarray[] Agrsout =Newmwarray[2]; //call the MATLAB function, 2 indicates the number of input parameters, before the output parameters need to add the REF keyword//This example implements matrix addition and subtraction for two three rows and two columns .Test. Matrixopera (2,refagrsout, Agrsin); //convert two output parameters to intermediate typeMwnumericarray Net_c = agrsout[0] asMwnumericarray; Mwnumericarray Net_d= agrsout[1] asMwnumericarray; C= (Double[,]) Net_c.toarray ();//successful conversion of output parameters to a two-dimensional arrayD = (Double[,]) Net_d.toarray (); //Be sure to pay attention to the conversion of the last C and D, different types of conversions vary greatly.//ToArray () corresponds to an array of n*m//tovetor () corresponds to a 1-d array//toscalardouble () corresponds to a single value for(inti =0; I <=2; i++)//validation of output results { for(intj =0; J <=1; J + +) {TextBox1.Text+ = C[i, j]. ToString () +" "; TextBox2.Text+ = D[i, j]. ToString () +" "; } TextBox1.Text+="\ r \ n";//line BreakTextBox2.Text + ="\ r \ n"; }
Mwnumericarray is the intermediate class of data conversion between Mwarray and C #, and realizes the function of passing parameters between C # and MATLAB functions.
Mwarray is the total type, the other starts with a MW, the variable type ending in array can be assigned or evaluated directly, it is the standard type of the post-compilation internal of the. m file, all C # types are ultimately converted to this type, and MATLAB functions can be called as parameters
When I run the above program, the first sentence instantiates the Mymathclass class with an error
But there's no problem with this statement alone.
Matprj.mymathclass test = new Matprj.mymathclass ();
Read about the error information, but the basic solution is to change a version of Matlab to solve, accidentally see said hresult:0x8007000b error and build platform some relationship, start to try is not because I this platform problem caused, sure enough, changed the platform for the build.
Specific solutions:
1: Build with property, right-click project name
2: Target platform selection x64
Run the program again with the following results:
The results are the same as those achieved in MATLAB, and the correctness of the results is verified.
Out of the MATLAB installation environment, how to realize the program is running freely:
Mixed programming through COM form, if the machine does not have MATLAB installed, compile generated software can still be used, but need to install MATLAB provided matlab Compiler Runtime (MCR), this software in the installation directory of MATLAB: x:\ Matlab\r2013a\toolbox\compiler\deploy\win32\mcrinstaller.exe (x is a drive letter installed by MATLAB), if you have MATLAB installed on your computer, you do not need to reinstall the software. In addition, the MATLAB version provided by different versions of Matlab Compiler runtime may be different, if you write your own software on another computer running a problem, may be different version of the compiler caused
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
C # and MATLAB mixed programming