C # mixed programming with MATLAB
Main reference: matlab c # book. I am reading the first version of the ebook and can search it online.
To be honest, I think C # has no advantage over MATLAB hybrid programming, basically through P/V
Invoke operates the C language DLL generated by the matlab mcc compiler. In addition, the speed is also slow and scary -_-
Below are four methods for Mixed Programming of C # and Matlab (which may not be complete enough. Please add them ):
1. Generate a C language DLL from the *. M file of MATLAB
Specific process:
1)
Write an M file in MATLAB (for example, a simple addition function myplus. m );
2) use mbuild of MATLAB
-Select the compiler for setup. the compiler on my machine has two lscs and vc2008 (Note that if vs.net is used for application, we recommend that you select vc2008 for the compiler; otherwise, the DLL cannot be found in C #.
Point)
3) Use MCC-B csharedlib: mypluslib myplus. m in the command line.
(If there are multiple m Files, write them later). Several files will be generated after dozens of seconds (*. C ,*. H ,*. DLL), where. the H file contains the exported function prototype.
4) create a C # project, copy the preceding DLL to the appropriate path, and use [dllimport (
"Mypluslib. dll", callingconvention = callingconvention. cdecl)]
Use the C # syntax to declare the functions in the. h file. In general, these functions include ** initialize, ** terminate, and the functions defined in the M file (** representing the Lib name ).
5) Use csharpmatlab compilerver4.cs to convert the Data Type in C # And the Matrix Type in MATLAB. This file defines the matlab csharp class. It mainly imports some functions in libmx. dll and encapsulates some practical functions such as double2mxarray_scalarreal.
2. C # Call the MATLAB work area calculation and return the result
Mainly use engopen, engclose, engputvariable,
Engevalstring and enggetvariable functions.
The general process is as follows:
Matlab csharp. engputvariable (Ep, "ml_a", mx_a );
Matlab csharp. engputvariable (Ep, "ml_ B", mx_ B );
Matlab csharp. engevalstring (Ep, "ml_c = ml_a + ml_ B ;");
Mx_c = maid. enggetvariable (Ep, "ml_c ");
3. Use MATLAB deploytool to generate COM components
Note: The old version is called comtool and is now called deploytool. In command
Enter deploytool in window.
1) select new deployment project-> MATLAB
Builder ne-> generic COM Component
2) Add the corresponding M file
3) Save and compile
4) create a C # project, add reference, OK
4. Use MATLAB deploytool to generate. NET components
1) select new deployment project-> MATLAB
Builder ne->. NET Component
2) Add the corresponding M file
3) Save and compile
4) create a C # project.
Using Mathworks. MATLAB. net. arrays;
Using
Mathworks. MATLAB. net. utility;
Therefore, you need to add the mwarray reference, which is generally installed with MATLAB builder.
Ne has it in GAC.
So much will be known for the time being ~